MySQL Django“无法添加或更新子行:外键约束失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6178816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django "Cannot add or update a child row: a foreign key constraint fails"
提问by Ram Rachum
I have a model Coupon
, and a model Photo
with a ForeignKey
to it:
我有一个模型Coupon
,以及一个Photo
带有 a的模型ForeignKey
:
class Photo(models.Model):
coupon = models.ForeignKey(Coupon,
related_name='description_photos')
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='images')
I set up inlines in the admin so now I'm able to add photos to a coupon from the admin.
我在管理员中设置了内联,所以现在我可以将照片添加到管理员的优惠券中。
I attempt to add one, and upload is successful, but then I get Django's debug page with this error:
我尝试添加一个,并且上传成功,但随后我得到 Django 的调试页面,并出现此错误:
IntegrityError at /admin/coupon/coupon/321/
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`my_project`.`coupon_photo`, CONSTRAINT `coupon_id_refs_id_90d7f06` FOREIGN KEY (`coupon_id`) REFERENCES `coupon_coupon` (`id`))')
What is this and how can I solve this problem?
这是什么,我该如何解决这个问题?
(If it matters, this is a MySQL database.)
(如果重要的话,这是一个 MySQL 数据库。)
EDIT:I tried it on an Sqlite3 database that has a slightly different dataset, and it worked, so perhaps there's loose data in my current DB? How can I find it and delete it?
编辑:我在一个数据集略有不同的 Sqlite3 数据库上进行了尝试,它工作正常,所以我当前的数据库中可能有松散的数据?我怎样才能找到它并删除它?
回答by Ram Rachum
Some of my tables were in InnoDB and some were in MyISAM... I changed everything to MyISAM and the problem was solved.
我的一些表在 InnoDB 中,一些在 MyISAM 中……我将所有内容都更改为 MyISAM,问题就解决了。
回答by mmrs151
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}
}
(According to the official doc) In previous versions of Django, fixtures with forward references (i.e. relations to rows that have not yet been inserted into the database) would fail to load when using the InnoDB storage engine. This was due to the fact that InnoDB deviates from the SQL standard by checking foreign key constraints immediately instead of deferring the check until the transaction is committed. This problem has been resolved in Django 1.4.
(根据官方文档)在以前版本的 Django 中,使用 InnoDB 存储引擎时,带有前向引用(即与尚未插入数据库的行的关系)的装置将无法加载。这是因为 InnoDB 通过立即检查外键约束而不是推迟检查直到事务提交,从而偏离了 SQL 标准。此问题已在 Django 1.4 中解决。
回答by ApPeL
To avoid this happening what you can also do is set your STORAGE_ENGINE
in your settings.py
为了避免这种情况发生,您还可以STORAGE_ENGINE
在 settings.py 中设置
for django >= 1.2
对于 Django >= 1.2
DATABASES = {
'default': {
...
'STORAGE_ENGINE': 'MyISAM / INNODB / ETC'
}
}
for django <= 1.2
对于 Django <= 1.2
DATABASE_STORAGE_ENGINE = "MyISAM / INNODB / ETC"
Please note this is only valid for MySQL
请注意这仅对 MySQL 有效
回答by supervacuo
I ran into this same issue: mmrs151's solution works, but NBthat, for Django <= 1.2
(i.e.before multiple database support), the setting looks like this:
我遇到了同样的问题:mmrs151 的解决方案有效,但请注意,对于Django <= 1.2
(即在多个数据库支持之前),设置如下所示:
DATABASE_OPTIONS = {"init_command": "SET foreign_key_checks = 0;"}
Worth noting that Ram Rachum seems to have worked around rather than solved the problem: MyISAM doesn't support transactions at all...
值得注意的是,Ram Rachum 似乎解决了问题,而不是解决了这个问题:MyISAM 根本不支持事务......
回答by GrvTyagi
Sometime the reason for this error is trying to save child table first and then save parent.
The solution for this is using.
有时此错误的原因是尝试先保存子表,然后再保存父表。
解决方案是使用。
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}
}
2). Check your database operation flow and make it parent ---> child
2)。检查您的数据库操作流程并使其parent ---> child
回答by Nikolay Georgiev
Another option is to drop the contraint in your MySQL table:
另一种选择是删除 MySQL 表中的约束:
alter table <TABLE_NAME> drop foreign key <CONTRAINT_NAME>;