0

我需要为我的 Reviewers_POS 表创建一个外键。我有 Reviewers_Id 作为我的主键,我想将它从 Reviewers_POS 表传递给我的 id_r1 ..

    import MySQLdb as mdb
    import sys


    conexao = mdb.connect('localhost', 'root', 'rot', 'DbOmelete')
    with conexao:  
    cur = conexao.cursor()

   cur.execute("CREATE TABLE IF NOT EXISTS Reviewers(Reviewers_Id int unsigned not null AUTO_INCREMENT, PRIMARY KEY (Reviewers_Id),Title varchar(500), Polarity INT, Review TEXT);")
   cur.execute("CREATE TABLE IF NOT EXISTS Reviewers_POS(ReviewersPOS_Id int unsigned not null PRIMARY KEY AUTO_INCREMENT, Review_POS TEXT,id_r1 integer,CONSTRAINT fk_id FOREIGN KEY (id_r1) REFERENCES Reviewers (Reviewers_Id));")

__ 我收到此错误:

回溯(最近一次通话最后):

  File "SQLTesteForeign.py", line 14, in <module>
    cur.execute("CREATE TABLE IF NOT EXISTS Reviewers_POS(ReviewersPOS_Id int unsigned not null PRIMARY KEY AUTO_INCREMENT, Review_POS TEXT,id_r1 integer,CONSTRAINT fk_id FOREIGN KEY (id_r1) REFERENCES Reviewers (Reviewers_Id) ON DELETE NO ACTION ON UPDATE NO ACTION);")
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1005, "Can't create table 'DbOmelete.Reviewers_POS' (errno: 150)")

有人知道如何解决吗?我在想我错过了什么..因为我真的不知道“哪里”是错误..

4

1 回答 1

2

您的主键是 type int unsigned,而您的外键是 type integer,两者不兼容。将您的外键更改为int unsigned,表将成功创建。

一个用于测试的 SQLfiddle

于 2013-05-24T18:11:42.840 回答