Illuminate\Database\QueryException: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
ID: php/laravel-migration-failed
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 11 | active | — | — | — |
Root Cause
Laravel migration failures occur when a migration tries to create a table that already exists, adds a duplicate column, violates a foreign key constraint, or contains a SQL syntax error. The migrations table may be out of sync with the actual database schema.
genericWorkarounds
-
87% success Use migrate:status to identify the failed migration and fix it manually
Run 'php artisan migrate:status' to see which migrations have run. If a migration partially failed, manually fix the database schema to match the expected state (e.g., drop the partially created table). Then run 'php artisan migrate' again. For production, write a new migration that handles the schema correction.
-
84% success Add conditional checks in migrations to handle existing schema objects
Use Schema::hasTable() and Schema::hasColumn() in migrations to check before creating. Example: 'if (!Schema::hasTable("users")) { Schema::create("users", ...); }'. For adding columns: 'if (!Schema::hasColumn("users", "email")) { Schema::table("users", function($t) { $t->string("email"); }); }'.
Dead Ends
Common approaches that don't work:
-
Running migrate:fresh in production to reset all tables
95% fail
migrate:fresh drops ALL tables and re-runs all migrations from scratch. In production this destroys all data including user accounts, transactions, and content. This is only safe in development environments.
-
Manually deleting rows from the migrations table to re-run a migration
78% fail
Deleting migration records without rolling back the actual schema changes causes the migration to attempt creating tables or columns that already exist, resulting in the same error. The migrations table must stay in sync with the actual schema.