Monday, January 4, 2016

Mysterious foreign key errors - MySQL Error 1215: Cannot add foreign key constraint"

I was trying to add a foreign key to table and I kept getting that silly MySQL error that doesn't provide much information: "MySQL Error 1215: Cannot add foreign key constraint".

The constraints were all done correctly, the data types all matched and I tested this on a different system with just the two tables involved in the foreign key constraint and it worked fine. However, when trying to add the foreign key constraint on the actual system I kept getting errors. I did some reading and someone posted here to look at SHOW ENGINE INNODB STATUS;

Near the top was an error from a table we had stopped using but was some how still related to the one of the tables that I was trying to add the foreign key constraint to. I dropped that other table because it had been deprecated and re-ran the code to add the foreign key constraint and voila it worked. :)

No comments:

Post a Comment