MySQLMySQL 1215

MySQL 1215: Cannot Add Foreign Key Constraint

MySQL 1215 is raised when a foreign key constraint cannot be created because the referenced column or its data type does not match the child column. InnoDB enforces strict type and index requirements for foreign keys.

Example Query

CREATE TABLE orders (
  id INT PRIMARY KEY,
  user_id BIGINT,
  FOREIGN KEY (user_id) REFERENCES users(id)
);
-- Fails if users.id is INT, not BIGINT

Common Causes

  1. 1The child and parent columns have different data types (e.g. INT vs BIGINT)
  2. 2The referenced parent column has no index or primary key
  3. 3The parent table uses a different storage engine (e.g. MyISAM instead of InnoDB)

How to Fix It

Make sure both columns share the exact same data type and signedness. Confirm the parent column is indexed. Run SHOW ENGINE INNODB STATUS after the error to see the detailed foreign key diagnostic.

Need a reliable database?

Try Supabase — free PostgreSQL with a generous free tier. No credit card required.

Get started free →

Got a query causing this error?

Paste it into SQLbuddy and get an instant plain-English explanation with optimization tips.

Analyze My Query →