prgrm
prgrm

Reputation: 3833

Laravel won't let me migrate a table because it already exists

I am trying to use Laravel Migration to create SQL tables but it won't let me.

Here is the error:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'mytable' already exists

Here is my code:

        Schema::create('mytable', function (Blueprint $table) {
            $table->increments('id');
            $table->foreign('othertable_id')
                ->references('id')->on('othertable')
                ->onDelete('cascade');
            $table->string('variable');
            $table->timestamps();
    });

    }

    {
        Schema::drop('mytable');
    }

I have also checked if other migrations are called "mytable" but they are not, so I am not sure where this come from.

I tried the following:

First

php artisan migrate:refresh

Which gave me an error

And then I deleted the entire database altogheter and used:

php artisan migrate

And still got the error

Upvotes: 7

Views: 17722

Answers (8)

Zulfikar Ahmad
Zulfikar Ahmad

Reputation: 504

in my case, I need to dump the SQL schema,

run this command:

php artisan schema:dump

then run PHP artisan migrate command again

Upvotes: 1

YelLOW
YelLOW

Reputation: 13

go to app>Providers>AppServiceProvider.php and copy paste this at the top:

use Illuminate\Support\Facades\Schema;

then go to function inside of it named 'Boot()' and copy paste this one:

Schema::defaultStringLength(191);

now go to your your database and delete your database completely, then go to console:

php artisan cache:clear
php artisan config:cache

then create a new database with same name and go back to consol and write :

 php artisan migrate (congrats your database now shows em all)

Upvotes: -2

Mohsen
Mohsen

Reputation: 4235

If you have the table in DB and dont want migrate create it, you can ignore it by check Schema::hasTable before create

public function up()
{
    if(Schema::hasTable('products')) return;       //add this line to migration file
    Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
});

}

Upvotes: 12

Rahul Rahatal
Rahul Rahatal

Reputation: 658

Try this,

Add following line :
Schema::dropIfExists('mytable'); inside the Up() function at the very beggining exactly before creating schema for the mytable.
i.e. before following code.
Schema::create('mytable', function (Blueprint $table)

Upvotes: 2

Nɪsʜᴀɴᴛʜ ॐ
Nɪsʜᴀɴᴛʜ ॐ

Reputation: 2904

I had neither removed the migration entry nor dropped the table manually from the database In my case, the solution is open up the tinker from the composer

$ php artisan tinker
>>> Schema::drop('users')
>>> Schema::drop('password_resets')
>>> Schema::drop('orders')
>>> exit
php artisan migrate

Here is the result of the above commands executed

nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate

In Connection.php line 647: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre ady exists (SQL: create table users (id int unsigned not null auto_incr ement primary key, name varchar(255) not null, email varchar(255) not n ull, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

In Connection.php line 449: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate:rollback
Nothing to rollback.
nishanth@localhost:~/Desktop/html/hutch$ php artisan tinker
Psy Shell v0.8.17 (PHP 7.1.20-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
>>> Schema::drop('password_resets')
=> null
>>> Schema::drop('orders')
=> null
>>> exit
Exit:  Goodbye.
nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2018_08_18_071213_create_orders_table
Migrated:  2018_08_18_071213_create_orders_table
nishanth@localhost:~/Desktop/html/hutch$ 

Also define the method down(), if it doesn't exist.
Otherwise, it'll show

SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'XYZ.ABC' (SQL: drop table ABC)

/**
  * Reverse the migrations.
  *
  * @return void
  */
public function down()
{
    Schema::dropIfExists('ABC');
}

Upvotes: 2

Mister Verleg
Mister Verleg

Reputation: 4293

In laravel 5.5 and > you can use:

  $ php artisan migrate:fresh 
  // ( not migrate:refresh wich is a different command)

This command first drops all tables and then reruns all migrations

Upvotes: 14

oseintow
oseintow

Reputation: 7371

do composer dump, remove the table manually from the database and also remove the migration entry for the the table you want to remove from the migration table and rerun the migration again to see what happens.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

The error says that the table mytable does already exist in the DB. You should rollback the migration:

php artisan migrate:rollback

And migrate again:

php artisan migrate

Upvotes: 1

Related Questions