MySQL Error 1215 — Why Is the Foreign Key Failing to Create?
Diagnosing unhelpful Error 1215 via SHOW ENGINE INNODB STATUS LATEST FOREIGN KEY ERROR, resolving signed/unsigned mismatches, collations, and leftmost index rules.
Operational Verification & Reliability Metadata
The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.
▸Symptom
ERROR 1215 (HY000): Cannot add foreign key constraintMySQL Error 1215 is notorious for being completely uninformative: It declines to indicate which table, column, or constraint violated relational rules.
However, InnoDB records the precise failure reason in its engine status log:
SHOW ENGINE INNODB STATUSGScroll to the `LATEST FOREIGN KEY ERROR` section:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2026-10-30 00:45:12 0x7f8 Error in foreign key constraint creation
for table `app`.`order_items`:
Foreign key constraint 'fk_items_orders' cannot be created:
referencing column `order_id` (bigint unsigned) is not compatible
with referenced column `id` (int)The exact incompatibility is explicitly detailed. Inspecting this section is mandatory when troubleshooting Error 1215.
▸Diagnostic Query: Side-by-Side Column Inspection
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE,
CHARACTER_SET_NAME, COLLATION_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'app'
AND ((TABLE_NAME = 'orders' AND COLUMN_NAME = 'id')
OR (TABLE_NAME = 'order_items' AND COLUMN_NAME = 'order_id'));▸The Seven Root Causes
- 1Signed vs Unsigned Integer Mismatch:
The single most common culprit. A parent BIGINT UNSIGNED column cannot be referenced by a child BIGINT (signed) column. Both consume 8 bytes, but their domains differ.
- 1Character Set or Collation Discrepancy:
String columns must match identical charsets and collations (utf8mb4_0900_ai_ci vs utf8mb4_general_ci will fail).
- 1Missing Leftmost Index on Referenced Column:
The referenced parent column must be the leftmost prefix of a key index.
- 1Existing Orphaned Records in Child Table:
When adding constraints via ALTER TABLE, if child rows exist with values absent from the parent table, creation fails.
- 1`ON DELETE SET NULL` on `NOT NULL` Column:
Contradictory semantic declaration.
- 1Non-InnoDB Storage Engine:
MyISAM tables do not support foreign keys. Both relations must run on InnoDB.
- 1Duplicate Constraint Name Across Schema:
Foreign key constraint identifiers must be unique schema-wide.
▸Step-by-Step Resolution
1. Reconcile Data Types and Character Sets
ALTER TABLE app.order_items
MODIFY order_id BIGINT UNSIGNED NOT NULL;2. Apply Foreign Key with Explicit Referential Actions
ALTER TABLE app.order_items
ADD CONSTRAINT fk_items_orders FOREIGN KEY (order_id)
REFERENCES app.orders (id) ON DELETE RESTRICT;SET FOREIGN_KEY_CHECKS = 0;. This masks existing orphaned child records and results in future runtime failures during routine DML operations.▸Related Error Guides & Hub Links
- •MySQL Error 1452 Foreign Key Constraint (H-14) — Parent row absence during insertion
- •MySQL Error 1451 Cannot Delete Parent Row (H-38) — Referential blocks during deletion
- •MySQL Error 1071 Specified Key Too Long (H-22) — InnoDB index key prefixes
- •MySQL Hub — Relational schema architecture and InnoDB engine internals
Other Database Error Triage Playbooks
Other battle-tested diagnostic guides for high-severity database errors.
MySQL ERROR 1045 Access denied: root Password Reset Guide
Four causes of MySQL 1045, meaning of using password: YES/NO, and safe credential recovery via --init-file.
MySQL Deadlock ERROR 1213: Graph Analysis and Permanent Prevention
Root causes of MySQL 1213 deadlock, parsing LATEST DETECTED DEADLOCK output, gap lock concurrency issues, and resilient application retry design.
MySQL Lock wait timeout exceeded: Finding Blocking Sessions
Finding the blocking transaction using performance_schema, safe KILL decision matrices, and permanent locking reduction.