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

August 13th, 2014

Warning: This post is 9 years old. Some of this information may be out of date.

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.