DBAtlas · Database operations atlas

1215: Cannot add foreign key constraint

MySQL

What does it mean?

The foreign key constraint being defined cannot be created; MySQL cannot add the constraint.

Why does it happen?

Data type/length/signedness (signed-unsigned) or character set/collation mismatch between the child and referenced columns, no suitable index/PRIMARY KEY on the referenced column, tables using different storage engines (one is MyISAM) or the referenced table not existing yet.

How to fix it

Make sure the column types on both sides are EXACTLY the same (e.g. both INT UNSIGNED). Verify that the referenced column is backed by an index (usually PRIMARY KEY or UNIQUE). Make sure both tables are InnoDB. Use the same charset/collation for string keys. For details: SHOW ENGINE INNODB STATUS (LATEST FOREIGN KEY ERROR).

Open in DBAtlas →

More from this engine