解决 mysql 插入数据报错: Cannot add or update a child row: a foreign key constraint fails

场景:我的情况是主表为用户 user 表,从表为职位 job 表,其中 job 表有一个外键为 user 表的主键。

我想要往 job 表中 insert 一条数据,报错如题。


原因:要插入 job 表的数据中外键列的值有问题,userId 字段的值在 user 表中找不到。


解决: 确保 job 表中要引用的外键值在 user 表中有对应数据就可以了。


“ you're adding a foreign key, you need to make sure that the data in the child table already exists in the parent table . ”

参考:https://www.experts-exchange.com/questions/28238845/1452-Cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails.html

猜你喜欢

转载自blog.csdn.net/u011314442/article/details/80762407