Fix for MySQL error Code 1217 – A foreign key constraint fails

August 13, 2014

Image for Fix for MySQL error Code 1217 – A foreign key constraint fails from unsplash.com

If you're trying to update or drop a table in MySQL and you're getting the error 'Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails' you need to toggle the foreign_key_checks variable first.

The reason for this error is because your tables have foreign keys which rely on the table you're attempting to change. Here's the fix for MySQL error Code 1217 – A foreign key constraint fails:

SET foreign_key_checks = 0;
DROP TABLE `stats`;
SET foreign_key_checks = 1;

I hope this helps someone else.