简体   繁体   English

无法添加或更新子行:Django生成的MySQL表上的外键约束失败

[英]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的项目上工作,该项目创建了两个表table1table2 ,并具有从table2table1的外键。 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_userid中不存在

Edit 编辑

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.

相关问题 django.db.utils.IntegrityError: (1452, '无法添加或更新子行:外键约束失败 - django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails Python Sqlalchemy mysql“无法添加或更新子行:外键约束失败” - Python Sqlalchemy mysql “Cannot add or update a child row: a foreign key constraint fails” Python 插入 MySQL:无法记录价格 1452 (23000):无法添加或更新子行:外键约束失败 - Python insert to MySQL : Failed to record Price 1452 (23000): Cannot add or update a child row: a foreign key constraint fails 错误1452(23000):无法添加或更新子行:外键约束失败我无法修复 - ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails That I can't fix IntegrityError(1452,'无法添加或更新子行:外键约束失败) - IntegrityError (1452, 'Cannot add or update a child row: a foreign key constraint fails) 在peewee中创建新模型实例时,如何修复“无法添加或更新子行:外键约束失败” - How to fix 'Cannot add or update a child row: a foreign key constraint fails' when creating new model instance in peewee MySQL 外键约束在 Django 应用程序中失败 - MySQL foreign key constraint fails in Django app 无法删除或更新父行:外键约束失败 SQL 错误 - Cannot delete or update a parent row: a foreign key constraint fails SQL error IntegrityError: (1451, '无法删除或更新父行:外键约束失败 (..)) - IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (..)) (pymysql.err.IntegrityError)(1451,'无法删除或更新父行:外键约束失败...') - (pymysql.err.IntegrityError) (1451, 'Cannot delete or update a parent row: a foreign key constraint fails…') in Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM