Zhong__[23000][1451] Cannot delete or update a parent row: a foreign key constraint fails

版权声明:个人原创 https://blog.csdn.net/anyedianxia/article/details/84582830

通过django创建了一对多的models类  生成了MySQL表  

在模型中也定义了models.ForeignKey(Your table name,on_delete=models.CASCADE)

在主表中删除有关联的数据时报错:

[23000][1451] Cannot delete or update a parent row: a foreign key constraint fails

经过认真思考并测试之后  明白了原因  不是on_delete=models.CASCADE未生效  而是直接操作的MySQL数据库  没有通过model操作其实就是直接操作数据库  on_delete=models.CASCADE也就不生效了  那么通过model操作删除数据时关联的表数据也会删除  所以问题在这里  通过django  models创建的数据库如果是一对多的关系型数据库  那么也要通过django  model操作数据库才会有期望效果  。。。  

猜你喜欢

转载自blog.csdn.net/anyedianxia/article/details/84582830