mysqldude
Error codes › 1215

MySQL & MariaDB error 1215: Cannot add foreign key constraint

MySQLCannot add foreign key constraint
MariaDBCannot add foreign key constraint for `…`
Example from our test runs: 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.

MySQLMariaDB
SymbolER_CANNOT_ADD_FOREIGNER_CANNOT_ADD_FOREIGN
SQLSTATEHY000HY000
TypeServer errorServer error
Versionsevery release since 5.0 activeevery 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 (INT vs INT UNSIGNED, INT vs BIGINT), 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.

  1. Connect with mysql -u root -p and create a scratch database and the test data:

    CREATE DATABASE demo;
    USE demo;
    CREATE TABLE parent (id INT PRIMARY KEY) ENGINE=InnoDB;
  2. 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

  1. 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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
✓✓✓✓✓✓✓✓✓
Child column type differs from the parent key (BIGINT vs INT) repro
MySQL 5.6✓ 12155.7✓ 12158.0✓ 37808.4✓ 37809✓ 3780
MariaDB 10.6✓ 100510.11✓ 100511.4✓ 100511.8✓ 1005
Referenced column has no index version check
MySQL 5.6✓ 12155.7✓ 12158.0✓ 18228.4✓ 61259✓ 6125
MariaDB 10.6✓ 100510.11✓ 100511.4✓ 100511.8✓ 1005
Use identical types (and signedness, charset) on both sides fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

Related errors

  • 3780 ER_FK_INCOMPATIBLE_COLUMNS
  • 1822 ER_FK_NO_INDEX_PARENT
  • 6125 ER_FK_NO_UNIQUE_INDEX_PARENT
  • 1005 ER_CANT_CREATE_TABLE
  • 1452 ER_NO_REFERENCED_ROW_2explained
How the message changed between versions
MariaDB fromSymbolMessage
5.5.68ER_CANNOT_ADD_FOREIGNCannot add foreign key constraint
10.3.39ER_CANNOT_ADD_FOREIGNCannot add foreign key constraint for `%s`