Mysql – DROP DATABASE fails due to foreign key constraints

foreign keyMySQLmysql-5.6

This is weird and I haven't ever encountered this.

I'm simply dropping a whole database.

$ mysql -u $RDS_USER -p$RDS_PASS -h $RDS_HOST -e "DROP DATABASE dev_test"

And yet it fails with this…

ERROR 1217 (23000) at line 1: Cannot delete or update a parent row: a foreign key constraint fails

I then go to the database itself and I see that most of the tables have been dropped except for two.

Why does this happen?

I'm using MySQL 5.6.

Best Answer

Try the DROP DATABASE a second time. Since most tables are gone, that may have relieved enough constraints to allow the last two tables to go.

Or... Do those two tables have FKs to each other? In this case, neither can be dropped "first". Hence, Greenforest35's answer (turn off FKs) seems to be the only solution.

Consider filing a bug report with http://bugs.mysql.com saying explaining that DROP DATABASE should be smarter -- such as ignoring the FKs automatically. (Or at least the ones that will be removed later by later tables -- this would take some analysis.)

Worse yet... Are there constraints between your remaining tables and table(s) in other database(s)?

(So many reasons not to bother with FKs.)