这似乎是一个常见错误,但对于我的生活,我无法弄清楚这一点。MySQL - 无法添加或更新子行:外键约束失败
我在MySQL中有一组InnoDB用户表,它们通过外键连接在一起;父表user
表和一组存储电子邮件地址,动作等的子表。这些都通过外键uid
连接到父表user
表,所有父键和子键都是int(10)
。
所有的子表都有一个uid
值,其外键约束指向user.uid
,并且设置为ON DELETE CASCADE
和ON UPDATE CASCADE
。
当我从user
中删除一个用户时,所有的子约束条目都被删除。然而,当我尝试更新user.uid
值,它会导致下面的错误,而不是级联uid
变化的子表:
#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
子表,因此我不相信这是表特定的错误。
编辑:
在结果中添加从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
'SHOW ENGINE INNODB STATUS' –
投票将其移至dba。 –