Menu

MySQL Error 1215: Cannot Add Foreign Key Constraint

Diagnose MySQL Error 1215 with a foreign-key checker for numeric and character columns, engines, and indexes, then inspect InnoDB’s latest error.

Posted on By Updated on
On this page

MySQL Error 1215 (HY000, ER_CANNOT_ADD_FOREIGN) means the server could not create a foreign key constraint. The message, Cannot add foreign key constraint, does not identify the cause. It commonly appears while creating a table or adding a constraint with ALTER TABLE. MySQL 8.4 has specific errors for common cases: Error 1822 for a missing parent index, Error 6125 for a missing unique parent key, and Error 3780 for incompatible columns. If the message names a missing parent-table index, see Error 1822 troubleshooting; if the referenced key is non-unique or partial, see Error 6125 troubleshooting; if child and parent column definitions are incompatible, see Error 3780 troubleshooting. Check the full message and server version before changing a schema. See the MySQL 8.0 error reference for Error 1215 and the MySQL 8.4 error reference for newer diagnostics.

If the message indicates a duplicate foreign-key constraint name, see the foreign-key constraint naming guide. The diagnostic can differ between MySQL versions.

Check common foreign-key mismatches

Use this local checker to compare integer and DECIMAL definitions, CHAR/VARCHAR character sets and collations, table engines, and the parent-table index. It does not connect to a database or send the values you enter. It checks only these conditions; it cannot prove that a foreign key will succeed. For composite keys or other data types, compare the full definitions and follow the checks below.

MySQL foreign-key checker

Enter the two column types and table details. For character columns, enter each column's effective character set and collation, including table defaults.

Enter only the base type (for example, INT UNSIGNED or VARCHAR(64)), not a full column definition. Supported type checks: integer, DECIMAL, CHAR, and VARCHAR. Integer display width is ignored; string lengths do not need to match.

A unique key with extra columns is a partial parent key for this reference. MySQL 8.4 rejects non-unique and partial parent keys by default. Enabling restrict_fk_on_non_standard_key=OFF permits this deprecated legacy behavior.

Start with matching parent and child columns

The foreign-key column in a child table must be compatible with the referenced column in the parent. For integer columns, match the type size and signedness. For nonbinary character columns, use the same character set and collation. The parent and child tables must also use the same storage engine; InnoDB is the usual choice. These rules are described in the MySQL foreign-key requirements.

For example, this definition uses INT UNSIGNED on both sides and gives the referenced column a primary key:

CREATE TABLE departments (
  department_id INT UNSIGNED NOT NULL,
  department_name VARCHAR(100) NOT NULL,
  PRIMARY KEY (department_id)
) ENGINE = InnoDB;

CREATE TABLE employees (
  employee_id BIGINT UNSIGNED NOT NULL,
  department_id INT UNSIGNED NOT NULL,
  PRIMARY KEY (employee_id),
  KEY ix_employees_department_id (department_id),
  CONSTRAINT fk_employees_department
    FOREIGN KEY (department_id)
    REFERENCES departments (department_id)
) ENGINE = InnoDB;

If one column were INT and the other INT UNSIGNED, align the definitions before adding the constraint. When altering a table, first check whether existing data can be converted safely; do not change signedness or width blindly on a production table.

Check the common causes

Compare both complete table definitions and check these details:

  1. Column types match. Integer width and signedness must match. For character columns, compare character set and collation as well as the type. Column names do not have to be the same.
  2. Both tables use the same supported engine. For InnoDB foreign keys, make sure neither table is using a different engine such as MyISAM. Temporary tables cannot participate in a foreign-key relationship.
  3. The referenced columns have a suitable key. On MySQL 8.4 with the default restrict_fk_on_non_standard_key=ON, the referenced columns must match every column of a PRIMARY KEY or UNIQUE key in the same order. Merely referencing a leftmost prefix of a longer unique key is a partial parent key and is rejected by default. This can produce Error 6125. Setting restrict_fk_on_non_standard_key=OFF permits non-unique or partial parent keys as deprecated legacy behavior. The child table also needs an index beginning with its foreign-key columns; InnoDB creates one automatically if needed.
  4. The referenced table and columns are the ones you intend. Confirm the active database, table names, column order, and spelling. Create the parent table before the child table when building a schema from scratch.
  5. The column and table features are supported. InnoDB foreign keys cannot use TEXT or BLOB columns, because those types require prefix indexes. User-partitioned InnoDB tables do not support foreign keys.

Run SHOW CREATE TABLE on both tables instead of relying on an ORM model or migration file; the live definitions may differ:

SHOW CREATE TABLE departments\G
SHOW CREATE TABLE employees\G

For a composite foreign key, check that the child and parent column lists pair up in the same order, and that the supporting indexes begin with those same columns. For example, (tenant_id, department_id) is a different key order from (department_id, tenant_id).

Read the detailed InnoDB error

When the error text does not reveal the mismatch, inspect InnoDB’s most recent foreign-key diagnostic immediately after the failed statement:

SHOW ENGINE INNODB STATUS\G

Find the LATEST FOREIGN KEY ERROR section. It can show the table, constraint, and reason InnoDB rejected the definition. This status contains the latest error, so run the command right after the failure before another foreign-key operation replaces the useful context. You can also run SHOW WARNINGS; immediately after the failing statement for additional server diagnostics.

Error 1215 versus Errors 1452 and 1451

Error 1215 concerns creating a foreign-key definition. Error 1452 occurs when an INSERT or UPDATE adds a child-row value that has no matching parent row; see how to fix MySQL Error 1452. Error 1451 is raised when a parent row cannot be updated or deleted because child rows still reference it. For the full constraint behavior, see the MySQL foreign-key guide.

Do not disable foreign_key_checks as a way to hide a malformed definition. It does not make incompatible column types or unsupported table definitions valid, and turning checks back on does not scan rows that were added while checks were disabled.