Cannot delete or update a parent row

HTTP Status 500 - Cannot delete or update a parent row: a foreign key constraint fails (`project`.`response`, CONSTRAINT `demand_id` FOREIGN KEY (`demand_id`) REFERENCES `demand` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

挺常见的一个问题了,前几天刚遇见过

问题描述:我要删除demand这个表中一行数据,但demand的这个表的主键是response表的外键,而且数据库定义时时ON DELETE 我选的NO ACTION ,ON UPDATE我也选的NO ACTION所以出现的效果就是,parent表中一行数据删了,可child表中没进行相应的删除操作,于是就报了上面的外键约束失败。这个错误是合理的,你不能把主键里的数据删了呀,你删完了,我child表去哪儿找数据呢?要删你得一起删啊!所以就级联删呗!

解:Cannot delete or update a parent row_第1张图片

单击foreign Keys选项卡,像我图中标记的那样改就可以了,项目文件不需要任何改动!!!

下面附上一段stack overflow的一个解释http://stackoverflow.com/questions/16163301/mysql-workbench-foreign-key-options-restrict-cascade-set-null-no-action-wh

Set NULL : Sets the column value to NULL when you delete the parent table row.

CASCADE : CASCADE will propagate the change when the parent changes. If you delete a row, rows in constrained tables that reference that row will also be deleted, etc.

RESTRICT : RESTRICT causes you can not delete a given parent row if a child row exists that references the value for that parent row.

NO ACTION : NO ACTION and RESTRICT are very much alike. when an UPDATE or DELETE statement is executed on the referenced table, the DBMS verifies at the end of the statement execution that none of the referential relationships are violated. in short child row no concern if parent row delete or update.



你可能感兴趣的:(Cannot delete or update a parent row)