php artisan migrate stops because the table already exists
SQLSTATE[42S01]: ... 1050 Table 'users' already existsShort answer
migrate tried to create a table the database already has. Run migrate:status. If the migration is Pending but the table exists, the table came from outside the migrations table, so guard or remove one side. If two pending files create the same table, delete the duplicate.
Fix it fast
1Find out which cause you have
Ask Laravel which migrations it thinks have run. A Pending line for the table named in the error tells you the cause.
php artisan migrate:status2Match what you see, then jump to its fix
What does the error look like?
SQLSTATE[42S01]: ... 1050 Table 'users' already exists
Cause 1 of 2
The table exists but its migration is not recorded as run
1Check
Laravel runs only the migrations that are not yet recorded. A table made by hand, by a SQL dump import, or by another app on the same database exists without a record.
php artisan migrate:status
mysql -e "SHOW TABLES LIKE 'users';" your_database2Fix
On a development database with no data you need, rebuild it: migrate:fresh drops all tables, then runs every migration. If the data matters, make the migration skip a table that is there, or drop only that table after a backup.
# development only: drops every table
php artisan migrate:fresh
// or guard the migration
public function up(): void
{
if (! Schema::hasTable('users')) {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
}3Verify
Run the migrations again and list them. All show Ran.
php artisan migrate
php artisan migrate:statusConfirmed by laravel.com
Cause 2 of 2
Two migration files create the same table
1Check
Each migration filename starts with a timestamp that sets the order. If a second file creates the same table, the first one runs and the second fails with 1050.
grep -rl "Schema::create('users'" database/migrations/2Fix
Keep one create-table migration for that table. Put later changes in a separate migration that alters the table, not creates it.
php artisan make:migration add_phone_to_users_table --table=users3Verify
Roll back the batch that failed if needed, then migrate.
php artisan migrate:rollback
php artisan migrateConfirmed by laravel.com
How do I stop it coming back?
- Never create tables by hand on a database that Laravel migrates.
- Write down() with Schema::dropIfExists so a rollback removes what up() created.
- Use a separate database per app, and use migrate:fresh only where data can be lost.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Databases
MySQL says the table already exists when you create it
ERROR 1050 (42S01): Table 'users' already exists
Error 1050 means a table with that name is already in the schema.
3 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fixBack end
Laravel: distinct() and count() give the total instead of the unique count
select('city')->distinct()->count() ignores the select
Laravel builds the count from the column you pass to count(), not from select().
2 causes, each with a fixBack end
Laravel Eloquent query for a column that is X or NULL
where('column', null) returns no rows
Wrap the pair in a closure: where(function ($q) { $q->where('col', $x)->orWhereNull('col'); }).
3 causes, each with a fixBack end
Laravel insert fails with Invalid datetime format 1292 Incorrect datetime value
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
MySQL ran in strict mode and refused a date or time value that it cannot store.
3 causes, each with a fix