Fix MySQL Error 1215 & 1452: Cannot Add Foreign Key Constraint
By Sharon Ben-Moshe · September 8, 2026 · 2 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Resolve MySQL Error 1215 and 1452. Discover how to fix column type mismatches, collation differences, missing parent indexes, and orphan records.
Quick answer
MySQL Error 1215 ('Cannot add foreign key constraint') occurs during DDL table creation or alteration when child and parent columns have incompatible types (such as INT vs BIGINT or signed vs unsigned), differing charsets/collations, or missing indexes on the parent. MySQL Error 1452 occurs during DML when inserted rows reference non-existent parent keys.
FAQs
How do I see the exact reason MySQL rejected a foreign key?
Run `SHOW ENGINE INNODB STATUS;` and inspect the 'LATEST FOREIGN KEY ERROR' section. It spells out the exact column mismatch, index absence, or type disagreement.
Can a signed INT reference an unsigned INT in MySQL?
No. In MySQL InnoDB, both the data type and the signedness must match identically. An INT UNSIGNED child column cannot reference an INT (signed) parent column.
What is the difference between Error 1215 and Error 1452?
Error 1215 is a DDL error when creating or altering a foreign key constraint. Error 1452 is a DML error when inserting or updating data that violates an existing foreign key constraint with orphaned references.
Can I temporarily disable foreign key checks in MySQL?
Yes, run `SET FOREIGN_KEY_CHECKS = 0;` before migrations or seed scripts, and re-enable it immediately afterward with `SET FOREIGN_KEY_CHECKS = 1;`.
Keep reading
Fix BigQuery "Unrecognized Name" Error: Scoping and Syntax Rules
By Sharon Ben-Moshe · Sep 29, 2026
Fix Google BigQuery's "Unrecognized name: column" error. Learn alias visibility rules across WHERE and QUALIFY, case sensitivity, and struct unnesting.
Read postSQLite "Datatype Mismatch" (Error 20): Type Affinity and Strict Tables
By Sharon Ben-Moshe · Sep 26, 2026
Understand SQLite Error 20 (datatype mismatch). Learn how SQLite type affinity, CHECK constraints, and STRICT tables enforce data integrity.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.