Django“无法添加或更新子行:外键约束失败"
我有一个模型 Coupon
和一个模型 Photo
带有 ForeignKey
:
I have a model Coupon
, and a model Photo
with a ForeignKey
to it:
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.
我尝试添加一个,并且上传成功,但随后我得到 Django 的调试页面并出现此错误:
I attempt to add one, and upload is successful, but then I get Django's debug page with this error:
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?
(如果重要的话,这是一个 MySQL 数据库.)
(If it matters, this is a MySQL database.)
我在一个数据集略有不同的 Sqlite3 数据库上进行了尝试,结果奏效了,所以也许我当前的数据库中有松散的数据?如何找到并删除它?
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?
推荐答案
我的一些表在 InnoDB 中,一些在 MyISAM 中...我将所有内容更改为 MyISAM,问题解决了.
Some of my tables were in InnoDB and some were in MyISAM... I changed everything to MyISAM and the problem was solved.
相关文章