1215 🐬 MySQL

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

  1. Align column definitions
  2. Index the parent column
  3. Check SHOW ENGINE INNODB STATUS
  4. Clean orphan rows first

Detailed fix by platform

MySQL

  1. SELECT c.* FROM child c LEFT JOIN parent p ON p.id = c.parent_id WHERE p.id IS NULL;

How to diagnose

  1. Types — Identical?
  2. Index — Parent column key?
  3. 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.