Skip navigation links

MySQL Forums :: Foreign Keys (Referential Integrity) :: Insert fails: error 1452, a foreign key constraint fails


Advanced Search

Insert fails: error 1452, a foreign key constraint fails
Posted by: Ken Iisaka ()
Date: February 15, 2011 06:06PM

I've encountered a weird infrequent problem with foreign key index as follows (5.5.9):

I have three tables: Text, KeywordToText and Text to represent n-m relations between text and keywords. PK are: uuid, (textUUID, keywordUUID) and uuid respectively.

select * from Text where uuid = 21198002

uuid, text
----------------
'21198002', 'relation'


select * from Keyword where uuid = 1902174

uuid, keyword
----------------
'1902174', 'relation'

Then,

INSERT INTO KeywordToText (keywordUUID, textUUID) VALUES (1902174, 21198002);

Error Code: 1452
Cannot add or update a child row: a foreign key constraint fails (`liveartsdev`.`KeywordToText`, CONSTRAINT `fk_KeywordToText_Keyword1` FOREIGN KEY (`keywordUUID`) REFERENCES `Keyword` (`uuid`) ON DELETE NO ACTION ON UPDATE NO ACTION)

I've also gotten:

Error Code: 1452
Cannot add or update a child row: a foreign key constraint fails (`liveartsdev`.`KeywordToText`, CONSTRAINT `fk_KeywordToText_Text1` FOREIGN KEY (`textUUID`) REFERENCES `Text` (`uuid`) ON DELETE NO ACTION ON UPDATE NO ACTION)

However, if I delete these foreign key constraints, and add them again, the INSERT works fine.

It seems that the indexes are corrupted, and somehow lost track of the PK values?



Edited 1 time(s). Last edit at 02/15/2011 07:48PM by Ken Iisaka.

Options: ReplyQuote


Subject Views Written By Posted
Insert fails: error 1452, a foreign key constraint fails 8735 Ken Iisaka 02/15/2011 06:06PM
Re: Insert fails: error 1452, a foreign key constraint fails 3435 Ken Iisaka 02/15/2011 08:17PM
Re: Insert fails: error 1452, a foreign key constraint fails 3190 Ken Britton 03/14/2011 02:46PM
Re: Insert fails: error 1452, a foreign key constraint fails 2968 Ken Iisaka 04/04/2011 06:27PM
Re: Insert fails: error 1452, a foreign key constraint fails 2624 Damien Lynn 04/05/2011 03:50PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.