MySQL - 无法添加或更新子行:外键约束失败

MySQL - Cannot add or update a child row: a foreign key constraint fails(MySQL - 无法添加或更新子行:外键约束失败)

本文介绍了MySQL - 无法添加或更新子行:外键约束失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个常见的错误,但我这辈子都想不通.

This seems to be a common error, but for the life of me I can't figure this out.

我在 MySQL 中有一组 InnoDB 用户表,它们通过外键绑定在一起;父 user 表和一组存储电子邮件地址、操作等的子表.这些都通过外键 绑定到父 user 表>uid,所有的父键和子键都是int(10).

I have a set of InnoDB user tables in MySQL that are tied together via foreign key; the parent user table, and a set of child tables that store email addresses, actions, etc. These are all tied to the parent user table by a foreign key, uid, with all of the parent and child keys being int(10).

所有子表都有一个 uid 值,外键约束指向 user.uid,并设置为 ON DELETE CASCADEON UPDATE CASCADE.

All of the child tables have a uid value with a foreign key constraint pointing to user.uid, and set to ON DELETE CASCADE and ON UPDATE CASCADE.

当我从 user 中删除用户时,所有子约束条目都将被删除.但是,当我尝试更新 user.uid 值时,会导致以下错误,而不是将 uid 更改级联到子表:

When I delete a user from user, all of the child constrained entries are removed. However, when I attempt to update a user.uid value, it results in the following error, rather than cascading the uid change to the child tables:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`accounts`.`user_email`, CONSTRAINT `user_email_ibfk_2` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE)

我有一种感觉,我必须在这里遗漏一些明显的东西.使用 user_email 删除键约束并尝试更新 user 中的值会导致相同的错误,但对于下一个按字母顺序排列的 user 子表,因此我不认为这是特定于表的错误.

I have a feeling I must be missing something obvious here. Removing the key constraint with user_email and attempting to update the value in user results in the same error but for the next alphabetical user child table, so I don't believe it is a table-specific error.

添加 SHOW ENGINE INNODB STATUS 的结果:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
121018 22:35:41 Transaction:
TRANSACTION 0 5564387, ACTIVE 0 sec, process no 1619, OS thread id 2957499248 updating or deleting, thread declared inside InnoDB 499
mysql tables in use 1, locked 1
17 lock struct(s), heap size 2496, 9 row lock(s), undo log entries 2
MySQL thread id 3435659, query id 24068634 localhost root Updating
UPDATE `accounts`.`user` SET `uid` = '1' WHERE `user`.`uid` = 306
Foreign key constraint fails for table `accounts`.`user_email`:
,
  CONSTRAINT `user_email_ibfk_2` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE
Trying to add in child table, in index `uid` tuple:
DATA TUPLE: 2 fields;
...
A bunch of hex code

But in parent table `accounts`.`user`, in index `PRIMARY`,
the closest match we can find is record:
...
A bunch of hex code

推荐答案

在一个不相关的任务上,我最近在 MySQL Workbench,在查看上述表的表关系时,我注意到我以前不知何故错过的重复"和/或虚假关系(它们没有出现在 PHPMyAdmin FWIW).删除这些额外的关系会立即解决问题.

On an unrelated task, I recently brought up our MySQL database in MySQL Workbench, and when viewing the table relations for the above tables, I noticed 'duplicate' and/or spurious relations that I had somehow missed before (they weren't showing up in PHPMyAdmin FWIW). Removing these extra relations cleared up the issue immediately.

这篇关于MySQL - 无法添加或更新子行:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:MySQL - 无法添加或更新子行:外键约束失败

基础教程推荐