這似乎是一個常見錯誤,但對於我的生活,我無法弄清楚這一點。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。 –