MySQL ERROR 1215 (HY000): Cannot add foreign key constraint
The foreign key can’t be created — mismatched column types, missing index, or incompatible engines/collations.
Seen on:
MySQL
Meaning
Same root causes as errno 150: types must match exactly (INT UNSIGNED vs INT), the parent column must be indexed, and both tables must be InnoDB.
Common causes
- Type/sign/length mismatch
- Parent column not a key
- Charset/collation mismatch
- Parent table missing
- Existing rows violating the FK
⚡ Quick fix
- Align column definitions
- Index the parent column
- Check SHOW ENGINE INNODB STATUS
- Clean orphan rows first
Detailed fix by platform
MySQL
SELECT c.* FROM child c LEFT JOIN parent p ON p.id = c.parent_id WHERE p.id IS NULL;
How to diagnose
- Types — Identical?
- Index — Parent column key?
- Data — Orphan rows?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026