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表去哪儿找数据呢?要删你得一起删啊!所以就级联删呗!

解:

单击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.



  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值