[英]Cannot add or update a child row: a foreign key constraint fails on a Django generated MySQL table
I am working on a django based project which created two tables table1
and table2
with a foreign key from table2
to table1
. 我正在基于django的项目上工作,该项目创建了两个表
table1
和table2
,并具有从table2
到table1
的外键。 I need to insert some values manually into table2
. 我需要手动将一些值插入
table2
。 On trying to do so in MySQL I get the constraint failure error. 在MySQL中尝试这样做时,出现约束失败错误。 I checked thoroughly, corresponding entries required for a foreign key constraint already exist in
table1
. 我仔细检查过,表
table1
已经存在外键约束所需的相应条目。 I looked into some previous questions on SO but could not find an appropriate solution. 我调查了有关SO的先前问题,但找不到合适的解决方案。
Table1 Schema: 表1架构:
+------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(100) | NO | | NULL | |
Table 2: 表2:
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| km | varchar(1000) | YES | | NULL | |
| owner_id | int(11) | NO | MUL | NULL | |
| receiver_id | int(11) | NO | MUL | NULL | |
+-------------+---------------+------+-----+---------+----------------+
The insert statement insert into crest_recipient values(id,owner_id=5,receiver_id=5,km="hello world");
insert语句
insert into crest_recipient values(id,owner_id=5,receiver_id=5,km="hello world");
or similar ones fail with the exact error as 或类似的失败,并出现确切的错误,因为
Cannot add or update a child row: a foreign key constraint fails (crest.crest_recipient CONSTRAINT crest_recipient_owner_id_4943116a1387be04_fk_crest_user_id FOREIGN KEY (owner_id) REFERENCES crest_user (id))
无法添加或更新子行:外键约束失败(crest.crest_recipient CONSTRAINT crest_recipient_owner_id_4943116a1387be04_fk_crest_user_id FOREIGN KEY(owner_id)参考crest_user(id))
You are trying to insert a row in child table crest_recipient which does not exist in parent table crest_user. 您正在尝试在子表crest_recipient中插入一行,该行在父表crest_user中不存在。
So first insert corresponding row in master table then you can insert in child table. 因此,首先在主表中插入相应的行,然后可以在子表中插入。
You are attempting to update or insert a row in crest.crest_recipient
table, a value in column owner_id
您正在尝试更新或插入一行
crest.crest_recipient
表中,列中的值owner_id
that does not exist in crest_user
column id
在
crest_user
列id
中不存在
make it look like this 使它看起来像这样
insert crest_recipient (km,owner_id,receiver_id) values ('test',5,1)
'test',5,1
are whatever you want. 'test',5,1
是您想要的。
Skip the id column. 跳过ID列。 Don't specify it, don't supply the value for it.
不指定它,不提供它的值。 It is an auto_increment PK most likely (basically there is nothing else it can be).
它最有可能是auto_increment PK(基本上没有别的办法了)。 The db engine chooses it for you.
数据库引擎会为您选择它。 Specifying it will just screw things up 99% of the time.
指定它只会使事情浪费99%的时间。
Focus on parameter 2 for now (the 5) 现在专注于参数2(5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.