# Laravel migration: SQLSTATE[HY000]: General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")

**Code:** `Foreign key constraint is incorrectly formed` · **Category:** Laravel · **Seen on:** Laravel, PHP, MySQL

MySQL can't create the foreign key because column types, order or engines don't match the referenced table.

## Meaning

The foreign column must exactly match the referenced column's type, size and signedness (`unsignedBigInteger` vs `bigIncrements` in Laravel 5.8+). The referenced table must exist first (migration order), and both must be InnoDB. Use `foreignId('user_id')->constrained()`.

## Common causes

- Referenced table created in a later migration
- Type mismatch (integer vs bigInteger, signed vs unsigned)
- MyISAM vs InnoDB engines
- Collation mismatch
- Referenced column not indexed/primary

## Quick fix

1. Use `$table->foreignId('user_id')->constrained()->cascadeOnDelete();`
2. Rename migration timestamps to run parents first
3. Match types and collation exactly

## Detailed fix by platform

### Laravel

1. `php artisan migrate:status` to confirm order; `SHOW CREATE TABLE users;` to inspect types

## How to diagnose

1. **Order** — Parent migrated first?
2. **Types** — Identical?
3. **Engine** — InnoDB?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/laravel/foreign-key-constraint-incorrectly-formed)
