Thursday, 18 October 2007

Foreign Key in MySQL Error #1452 and #1216

This has caused me 3-4 hours of time wasting and it is so bloody simple! I can't believe after googling for so many hours that I could not find something that could solve this problem for me.

If you've came across an error #1452 when trying to add a foreign key and you have existing entries in the table, you need to make sure there are valid values in all entries in the foreign key column before creating a foreign key.

So what I did was do a zero fill on the foreign key column. Sweet I thought. This should fix it. After trying to create the foreign key again, I get an error #1216. Turns out that the values in the foreign key column must be valid (i.e. correspond to a valid id to referenced table) and the zero fill obviously didn't help because I dont have any id with 0.

Once you've entered valid values in, the foreign key creation should work! Make sure that the column in the referenced table is a primary key or an index and foreign key column is an index before proceeding.

41 comments:

Anonymous said...

Just what I was looking for !
Thanks, very helpfull !!

Anonymous said...

Thanks for the tip.
I still have some issues when I try to add foreign keys. By chance, do you have any idea of the cause of error #1005

Anonymous said...

Hey dude thank you for this topic !!

It was pretty useful to me !

You came from heaven !

Anonymous said...

Really thank you! Congratulations that you publish the solution! I was desparate but you saved me!

Danny Ng said...

No worries, I'm glad this solution helped you guys.

Palaniappan said...

Thank you. Seriously.

Anonymous said...

Very helpful - sorted my prob after 2 hours googling in vain.

Sindhu Kumari said...

Hey, It works..Thanks a lot.. It was really helpful..

Kelvin R CHENG said...

So helpful. Thank you heaps

Unknown said...

Oh my, thank you for this.

How insane, I just wasted two hours until I found this.

Anonymous said...

TYVM!

Anonymous said...

yes, thank u really much for this :)

Anonymous said...

Really dumb... I wasted like 3 hours as well trying to fix this error. Thanks a lot!!!

Anonymous said...

Thanks also, this explanation finally helped - better than the useless descriptions on the mysql forums that just tell people to get stuffed.

Anonymous said...

Yeah, THANK YOU, THANK YOU! REALLY HELPFUL AND HOUR-SAVING!
About error #1005:
1. Check your database type. Be sure they are both InnoDB.
2. Check your field types. They must be absolutely the same - for example INT (11) NOT NULL or whatever but both field types must be absolutely the same
3. Keep asking Google ;)

Anonymous said...

Thank you!

With the information you provided, I was able to track down the error in my database. My foreign key field type did not match the primary key field type. The resulting data some field became truncated and therefore did not match.

For anyone else, check to make sure that your field types match in both tables! :)

Thanks again for the information!!

Anonymous said...

ty much, I had the same problem, spent several minutes double checking the database!

Anonymous said...

Thanks a lot, it fixed my problem too.

Anonymous said...

This post just saved me hours of frustrasion. Thanks a lot!

Help said...

I don't understand...
you say:

"Once you've entered valid values in, the foreign key creation should work!"

What you mean by valid? Of course they are valid, I have primary ids and the foreign keys are valid (they exist in the parent table).

Could you be more specific?

I can't get out of this problem. Both tables columns are exact same, except the parent is a primary key and auto_increment.

Anonymous said...

Wow! I spent 3-4 hours digging through the web and trying every trick I could find, and this was the solution. Many thanks for putting this up.

Anonymous said...

Thank you, thank you, the angels sing your name.

clauds said...

hey, thanks! i needed that answer so badly.. good thing you posted!

thanks! =)

Anonymous said...

Grrrrrrrrrrrrrrrrrrrrrr!! Where were you six hours ago??!! Thank you so much for this very very useful tip. Finally!

Anonymous said...

THANKS! I was almost ready to give up and restructure my db :) That worked like a charm!

gus said...

thank you for this and for the message on the captcha!

Unknown said...

Thanks a lot - after 2 wasted hours u did the trick

Anonymous said...

nice solution. I wrote sql code to see the inconsistence:
SELECT * FROM table1 where id not in (select id from table2 );

Anonymous said...

Nice - real helpful - THANKS

Anonymous said...

Thank you! This was just information I needed!

Well said!

John Ortiz OrdoƱez said...

Thanks for your idea... Now I'm trying to resolve a problem when I insert data to some tables... g'bye!

KR said...

Thanksssssssssssssss

Anonymous said...

Thank you very much - finally. Just feal so stupid now that it was so simple.. :)

Evan said...

you're right, the solution is obvious. thanks

Sarah Lewis said...

Thanks for taking the time to post your observations. You saved me hours of head-banging and I'm very grateful! :)

Mohammad Sani Suprayogi said...

great! thanks...

Anonymous said...

Really Superb :)

Anonymous said...

Thankyou... :)

FDisk said...
This comment has been removed by the author.
FDisk said...

Thanks allot. My problem was engine type difference

chandu said...

thanku very much....