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