1924
MariaDBERRORNotableDDLHIGH confidence

Cannot drop index needed in a foreign key constraint

Production Risk

Medium — foreign key enforcement is intact; fix the schema carefully.

What this means

MySQL prevents dropping an index that is required to enforce a foreign key constraint. InnoDB uses the index to perform constraint checks efficiently.

Why it happens
  1. 1DROP INDEX on an index that backs a FOREIGN KEY constraint on the same table.
  2. 2ALTER TABLE DROP INDEX when a FK references the indexed column(s).
How to reproduce
trigger — this will error
trigger — this will error
ALTER TABLE orders DROP INDEX customer_id_idx; -- customer_id_idx supports a FK

expected output

ERROR 1924 (HY000): Cannot drop index 'customer_id_idx': needed in a foreign key constraint.

Fix

Drop the foreign key first, then drop the index

Drop the foreign key first, then drop the index
ALTER TABLE orders DROP FOREIGN KEY fk_customer, DROP INDEX customer_id_idx;

Why this works

Removing the FK constraint frees the index from being required, allowing it to be dropped.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1924 ER_DROP_INDEX_FK2

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MariaDB errors