【发布时间】:2012-10-09 14:50:00
【问题描述】:
这似乎是一个常见的错误,但我这辈子都想不通。
我在 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
【问题讨论】:
-
投票将其移至 dba。
标签: mysql foreign-keys mysql-error-1452