1005 🐬 MySQL

MySQL ERROR 1005 (HY000): Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")

A foreign key definition is invalid — the referenced column’s type, index or charset doesn’t match.

Seen on: MySQL

Meaning

FK columns must have identical types (including UNSIGNED and length), the referenced column must be indexed (usually a primary/unique key), and both tables must be InnoDB with compatible collations.

Common causes

  • INT vs INT UNSIGNED / BIGINT mismatch
  • Referenced column not indexed
  • Different charset/collation on string keys
  • Referenced table doesn’t exist yet (creation order)
  • MyISAM table

⚡ Quick fix

  1. Make column definitions identical
  2. Index the referenced column
  3. Create parent tables first
  4. Run SHOW ENGINE INNODB STATUS for the detailed reason

Detailed fix by platform

MySQL

  1. SHOW ENGINE INNODB STATUS\G -- see LATEST FOREIGN KEY ERROR

How to diagnose

  1. Types — Child vs parent column definitions
  2. Index — Referenced column indexed?
  3. Order — Parent created first?

🧠 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.