MySQL & MariaDB error 1215: Cannot add foreign key constraint
Cannot add foreign key constraint (5.6.51) InnoDB refused to create a foreign key. The parent and child columns don't match, or the parent key has no suitable index. The error number for this depends on your version.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_CANNOT_ADD_FOREIGN | ER_CANNOT_ADD_FOREIGN |
| SQLSTATE | HY000 | HY000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
The generic Cannot add foreign key constraint gives no reason. Newer versions split it into specific errors, so the same mistake has a different number per version. Our tests show:
| Cause | MySQL 5.6/5.7 | MySQL 8.0 | MySQL 8.4/9.x | MariaDB |
|---|---|---|---|---|
| Column types differ (BIGINT vs INT) | 1215 | 3780 | 3780 | 1005 (errno 150) |
| Parent column not indexed / not unique | 1215 | 1822 | 6125 | 1005 (errno 150) |
MySQL 8.4 is also stricter: a foreign key must reference a PRIMARY KEY or UNIQUE key, not just any index (restrict_fk_on_non_standard_key).
Why it happens
- The child and parent columns have different types, lengths or signedness (
INTvsINT UNSIGNED,INTvsBIGINT), or different character sets / collations for strings. - The referenced column has no index, or (MySQL 8.4+) no unique index.
- One of the tables isn’t InnoDB (MyISAM silently ignores foreign keys).
- The parent table doesn’t exist yet, often table creation order in a script.
- The child data already contains values missing from the parent: that’s 1452.
How to fix it
- Compare both column definitions with
SHOW CREATE TABLE parent; SHOW CREATE TABLE child;and make them identical (type,UNSIGNED, charset and collation). - Make sure the referenced column is the PRIMARY KEY or has a UNIQUE index.
SHOW ENGINE INNODB STATUS\G→ “LATEST FOREIGN KEY ERROR” explains the exact reason on every version.- Create parent tables before children, or load a dump with
SET foreign_key_checks = 0.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB;Run:
CREATE TABLE child (id INT PRIMARY KEY, parent_id BIGINT, FOREIGN KEY (parent_id) REFERENCES parent(id)) ENGINE=InnoDB;
You get: Cannot add foreign key constraint(5.7.44)
Verified fixes
Use identical types (and signedness, charset) on both sides
CREATE TABLE child (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES parent(id)) ENGINE=InnoDB; SELECT COUNT(*) FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE();
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Child column type differs from the parent key (BIGINT vs INT) repro | ✓ 1215 | ✓ 1215 | ✓ 3780 | ✓ 3780 | ✓ 3780 | ✓ 1005 | ✓ 1005 | ✓ 1005 | ✓ 1005 |
| Referenced column has no index version check | ✓ 1215 | ✓ 1215 | ✓ 1822 | ✓ 6125 | ✓ 6125 | ✓ 1005 | ✓ 1005 | ✓ 1005 | ✓ 1005 |
| Use identical types (and signedness, charset) on both sides fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
How the message changed between versions
| MariaDB from | Symbol | Message |
|---|---|---|
| 5.5.68 | ER_CANNOT_ADD_FOREIGN | Cannot add foreign key constraint |
| 10.3.39 | ER_CANNOT_ADD_FOREIGN | Cannot add foreign key constraint for `%s` |