gpt4 book ai didi

python - Mysql 创建外键

转载 作者:行者123 更新时间:2023-11-29 08:30:31 24 4
gpt4 key购买 nike

我需要为我的 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)")

有人知道怎么解决吗?我想我错过了一些东西..因为我真的不知道错误“在哪里”..

最佳答案

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

An SQLfiddle to test with

关于python - Mysql 创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com