3408
MariaDBERRORNotableForeign KeysHIGH confidence

Foreign key parent table has no index for the referenced column

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(unindexed_col);

expected output

ERROR 3408 (HY000): Cannot add foreign key; referenced column has no index.

Fix

Add unique index to parent referenced column

Add unique index to parent referenced column
ALTER TABLE parent ADD UNIQUE INDEX idx_col (unindexed_col); ALTER TABLE child ADD FOREIGN KEY (col) REFERENCES parent(unindexed_col);

Why this works

A unique index on the parent column satisfies the InnoDB FK referential integrity requirement.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3408 ER_FK_NO_INDEX_PARENT

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

← All MariaDB errors