这似乎是一个常见的错误,但对于我的生活,我无法弄清楚这一点。
我在 MySQL 中有一组 InnoDB 用户表,它们通过外键绑定在一起;父user
表,以及一组存储电子邮件地址、操作等的子表。这些都user
通过外键 绑定到父表,uid
所有父键和子键都是int(10)
。
所有的子表都有uid
一个外键约束指向user.uid
并设置为ON DELETE CASCADE
and的值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