Menu

1 / 2

MySQL Cannot Drop Index Needed in a Foreign Key Constraint: Which Fix to Use

DEV Community: laravel·Son Tran·4 days ago
#5v0RY69n
#devtolaravel#devto#dev#index#foreign
Long article (1769 words) — no AI summary yet.
Reading 0:00
15s threshold

Disclosure: I build Schemity , a desktop ERD tool - this post is from our blog and uses it for the examples.

TL;DR: MySQL refuses to drop the last index whose leading columns hold a foreign key's columns, and turning off foreign_key_checks does not change that on MySQL 8.4. If the foreign key is going too, drop it and the index in one ALTER TABLE . If the key stays, add the replacement index in the same statement as the drop. The trap is an index MySQL swapped in for its own without telling you, which turns a routine rollback into error 1553.

MySQL error 1553, Cannot drop index 'customer_id': needed in a foreign key constraint , means the index you are dropping is the last one MySQL can use to enforce a foreign key, and the fix depends on whether that foreign key should survive the migration. If the key is going too, drop both in one statement. If the key stays, put a replacement index in first.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.