In my Laravel app, I have connections to different databases and when I need to fresh my tables in each of database with a new dummy data (I do it with php artisan migrate:fresh --seed command) I always have this error

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

which is weird because I have down() method in my migration class

class CreateExampleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('conn2')->create('table_name', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection('conn2')->dropIfExists('table_name');
    }
}

1 Answers

0
party-ring On

If you use migrate:refresh instead of fresh it will rollback the database using your down() methods.

I think the problem is not in your down() but with your up() : it is trying to create a table, but a table with that name already exists.

I would suggest doing something like:

if (!Schema::hasTable('tbl_name')) {
     // create table
}

You may also have to specify which database you are using in the migrations.