Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JohnnyBigodes's avatar

Laravel Migrations on a second connection, goes wrong?!?!

Hello everyone,

first of all I have 2 different connections on my Laravel App.

I need this to write migrations to 2 different Databases on the same Server.

When I do the first migration everything goes like it should and even the seeders work without a problem.

But when I try to do a php artisan migrate:fresh I get this error.

   Illuminate\Database\QueryException

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'countries' already exists (SQL: create table `countries` (`id` bigint unsigned not null auto_increment primary key, `code` varchar(2) not null, `name` varchar(50) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

      +8 vendor frames
  9   database/migrations/2021_08_02_132415_create_countries_table.php:23
      Illuminate\Database\Schema\Builder::create()

      +32 vendor frames
  42  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

This is my Migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCountriesTable extends Migration
{
    protected $connection = 'con2';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('con2')->create('countries', function (Blueprint $table) {
            $table->id();
            $table->string('code', 2)->index();
            $table->string('name', 50);
            $table->timestamps();
        });
    }

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

This is my config\database.php:

       'con1' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_CON1', 'con1_db'),
            'username' => env('DB_USERNAME_CON1', 'forge'),
            'password' => env('DB_PASSWORD_CON1', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'con2' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE_CON2', 'con2_db'),
            'username' => env('DB_USERNAME_CON2', 'forge'),
            'password' => env('DB_PASSWORD_CON2', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

What am I doing wrong. I set the right connection in the migration.

The first time it runs like a charm, but on a refresh not.

Thank you in advance

0 likes
7 replies
ItsClassified's avatar

I see 2 different things, the migration file of the 'regions' and the error about the 'countries' table.

What the error tells you is that the table countries already exists, if that happends during migrate:fresh that could mean that your trying to create the table countries twice.

The migration you showed is not related, since that is talking about a table: regions instead of countries

JohnnyBigodes's avatar

Sorry, you are right... I got the wrong migration.

But wouldnt php artisan migrate:fresh drop the table and try to create it again? It seems that the table isnt dropping for some reason.

Here is the country migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCountriesTable extends Migration
{
    protected $connection = 'con2';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('con2')->create('countries', function (Blueprint $table) {
            $table->id();
            $table->string('code', 2)->index();
            $table->string('name', 50);
            $table->timestamps();
        });
    }

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

EDIT: Edited the migration on the first post, to be "countries". Sorry

JohnnyBigodes's avatar

Thank you... Yeah I tried your solution, but still on the second run I will get a message saying my table already exists.

This is just a pain to handle.

Even if I make a schema:dump, will still get this problems.

Sometimes I think, that Laravel is not meant to work with more than one connection.

ItsClassified's avatar

I have multiple apps that use multiple connections, however I never had a application that had to do mirgations for multiple connections :/ may i ask why you want it in the first place?

JohnnyBigodes's avatar

That is exactly what I was asking and found out it isnt possible.

Now I still have other connections, but I separate the migrations on specific folders.

When migrating I just have to add --database=con1 or con2 and the --path=migrations/con1 to do this.

It is still a pain, because the connection is in the migrations and seeders and this could be separated automaticaly, but I can live with doing it myself.

Thank you

1 like

Please or to participate in this conversation.