Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

php artisan migrate stops because the table already exists

ErrorSQLSTATE[42S01]: ... 1050 Table 'users' already exists

Short 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.

  • Applies to Laravel 12, PHP 8.3, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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:status
  2. 2Match what you see, then jump to its fix

    1. 1migrate:status shows the create-table migration as Pending, and the table is in the databaseThe table exists but its migration is not recorded as runFix
    2. 2migrate:status shows two Pending files that both call Schema::create for the table in the errorTwo migration files create the same tableFix

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_database

2Fix

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:status

Confirmed by laravel.com

Did this fix it?

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=users

3Verify

Roll back the batch that failed if needed, then migrate.

php artisan migrate:rollback
php artisan migrate

Confirmed by laravel.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.