Skip to content
Databases3 min read

Fix MySQL error 1215: Cannot add foreign key constraint

ERROR 1215 (HY000): Cannot add foreign key constraint

Short answer

Error 1215 means MySQL refused to create a foreign key because the two columns are incompatible or the referenced table isn't ready. The usual causes are a type mismatch such as INT versus BIGINT UNSIGNED, different collations, or creating the table before the one it points to.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

A foreign key ties a column in one table to a column in another, such as orders.user_id pointing to users.id. MySQL only accepts it when the two columns match exactly and the referenced table exists and is indexed. The wording depends on the server:

-- MySQL 5.6 / 5.7
ERROR 1215 (HY000): Cannot add foreign key constraint
-- MariaDB
ERROR 1005 (HY000): Can't create table `cpuser_shop`.`orders` (errno: 150 "Foreign key constraint is incorrectly formed")
-- MySQL 8 (when the column types differ)
ERROR 3780 (HY000): Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible.

Causes

  • Different type or sign: INT versus BIGINT, or UNSIGNED versus signed. In Laravel, $table->id() creates BIGINT UNSIGNED, while $table->integer('user_id') creates a signed INT.
  • Different charset or collation on string columns, such as utf8mb4_unicode_ci versus latin1_swedish_ci.
  • No index on the referenced column: it must be a primary key, unique or indexed.
  • The referenced table doesn’t exist yet: migration order, or table order in an import file.
  • A non-InnoDB engine: MyISAM doesn’t support foreign keys.
  • ON DELETE SET NULL on a NOT NULL column.
  • Existing rows that break the relationship when adding the key to a populated table, which usually shows up as error 1452.

How to fix it

  1. Compare both definitions in phpMyAdmin’s SQL tab:
SHOW CREATE TABLE users;
SHOW CREATE TABLE orders;

Check the type, UNSIGNED, collation and ENGINE=InnoDB.

  1. Match the type, then add the key:
ALTER TABLE orders MODIFY user_id BIGINT UNSIGNED NOT NULL;
ALTER TABLE orders
  ADD CONSTRAINT orders_user_id_foreign
  FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE;
  1. In Laravel, use the helper that matches the type automatically:
$table->foreignId('user_id')->constrained()->cascadeOnDelete();

And make sure the users migration has an earlier timestamp in its filename than the orders migration.

  1. Change the engine if it’s MyISAM:
ALTER TABLE orders ENGINE=InnoDB;
  1. Find orphaned rows before adding a key to a table with data:
SELECT o.id, o.user_id FROM orders o
LEFT JOIN users u ON u.id = o.user_id
WHERE u.id IS NULL;

Delete or correct them, then add the key.

  1. When importing a full dump, suspend key checks. On phpMyAdmin’s Import page untick Enable foreign key checks, or wrap the file:
SET FOREIGN_KEY_CHECKS=0;
-- ... dump contents
SET FOREIGN_KEY_CHECKS=1;
  1. For more detail on your own server, SHOW ENGINE INNODB STATUS; includes a LATEST FOREIGN KEY ERROR section. It needs the PROCESS privilege, which shared hosting accounts normally don’t have.

Prevention

  • Use foreignId() and constrained() in Laravel instead of declaring types by hand.
  • Create every table with the same charset, preferably utf8mb4_unicode_ci, as covered in Incorrect string value.
  • Keep InnoDB as the default engine for all tables.

On ArabyHost

Databases run on MariaDB/MySQL, so you’ll usually see the errno: 150 wording rather than 1215; the fix is the same. Compare tables and run the SQL above in phpMyAdmin, and on AI/Developer plans run php artisan migrate over SSH, as in the Laravel deployment guide. If you can’t tell which migration failed, the Araby AI assistant reads your error log and points to the mismatched columns.

FAQ

What does errno: 150 Foreign key constraint is incorrectly formed mean?

It's MariaDB's wording for the same problem: the foreign key doesn't match the column it references, usually in type, UNSIGNED or collation, or the referenced column has no index.

Why does my Laravel foreign key fail when both tables exist?

Usually because users.id is BIGINT UNSIGNED while you defined user_id with integer(). Use foreignId('user_id'), which creates the matching type for you.

Is turning off FOREIGN_KEY_CHECKS safe?

Yes while importing a complete, valid dump, where table order is the only issue. Don't leave it off in normal operation, because it lets rows in without a valid relationship.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.