问题描述
我有一个模型优惠券
和一个模型照片
与 ForeignKey
给它:
class Photo(models.Model):
coupon = models.ForeignKey
related_name ='description_photos'
title = models.CharField(max_length = 100)
image = models.ImageField(upload_to ='images')
我在管理员中设置了内联,所以现在我可以从管理员添加照片到优惠券。
我尝试添加一个,上传成功,但是我得到Django的调试页面,出现这个错误:
code> IntegrityError at / admin / coupon / coupon / 321 /
(1452,'无法添加或更新子行:外键约束失败(`my_project`.`coupon_photo`,CONSTRAINT`coupon_id_refs_id_90d7f06` FOREIGN KEY(`coupon_id`)参考`coupon_coupon`(`id`))')
什么是这个怎么解决这个问题? / p>
(如果重要,这是一个MySQL数据库。)
编辑:我在一个稍微不同的数据集的Sqlite3数据库上尝试过,它的工作,所以也许在我当前的数据库中有松散的数据?我如何找到它并删除它?
我的一些表在InnoDB中,有些在MyISAM ...我将所有内容更改为MyISAM,问题已解决。
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.
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?
(If it matters, this is a MySQL database.)
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?
Some of my tables were in InnoDB and some were in MyISAM... I changed everything to MyISAM and the problem was solved.
这篇关于Django“无法添加或更新子行:外键约束失败”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!