简体   繁体   中英

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

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

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');
}

Try this,

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

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

执行composer dump ,从数据库中手动删除表,并删除要从迁移表中删除的表的迁移条目,然后再次重新运行迁移以查看发生的情况。

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();
});

}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM