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:
INTversusBIGINT, orUNSIGNEDversus signed. In Laravel,$table->id()createsBIGINT UNSIGNED, while$table->integer('user_id')creates a signedINT. - Different charset or collation on string columns, such as
utf8mb4_unicode_civersuslatin1_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 NULLon aNOT NULLcolumn.- 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
- Compare both definitions in phpMyAdmin’s SQL tab:
SHOW CREATE TABLE users;
SHOW CREATE TABLE orders;
Check the type, UNSIGNED, collation and ENGINE=InnoDB.
- 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;
- 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.
- Change the engine if it’s MyISAM:
ALTER TABLE orders ENGINE=InnoDB;
- 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.
- 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;
- For more detail on your own server,
SHOW ENGINE INNODB STATUS;includes aLATEST FOREIGN KEY ERRORsection. It needs the PROCESS privilege, which shared hosting accounts normally don’t have.
Prevention
- Use
foreignId()andconstrained()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.