3407
MariaDBERRORNotableForeign KeysHIGH confidence

Foreign key child table has no index for the constraint

Production Risk

Low — DDL fails; no constraint is created.

How to reproduce
trigger — this will error
trigger — this will error
ALTER TABLE child ADD FOREIGN KEY (col) REFERENCES parent(id);

expected output

ERROR 3407 (HY000): Failed to add the foreign key constraint. Missing index for constraint.

Fix

Create index on child FK column

Create index on child FK column
ALTER TABLE child ADD INDEX idx_fk_col (col); ALTER TABLE child ADD FOREIGN KEY (col) REFERENCES parent(id);

Why this works

InnoDB can now use the index for efficient FK constraint checking.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3407 ER_FK_NO_INDEX_CHILD

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

← All MariaDB errors