Description
Laravel Version
12.1.1
PHP Version
laravelsail 8.4
Database Driver & Version
laravelsail mariadb:11
Description
I encountered a problem when using Laravel (with Sail) with multiple database connections. Specifically, when I run the sail artisan migrate:fresh
command, it only drops tables from the main database (as expected), but it also runs migrations for additional connections, such as the conB connection, even when I specify a particular connection using the --database flag.
Problem overview
In my Laravel project, I have configured multiple database connections in config/database.php
(based on this), including a main database (mariadb) and a secondary connection (conB), using Schema::connection('conB')->create(...) in my migrations.
When running the command:
sail artisan migrate:fresh --database=mariadb
or just
sail artisan migrate:fresh
I expect that only the migrations for the mariadb connection will run and the conB connection will remain unaffected. However, the migrations for conB are also being executed, despite explicitly specifying the database connection to use.
This leads to an issue where the secondary database connection (conB) is being migrated, even though it wasn't intended to be part of the migrate:fresh command and throw an error SQLSTATE[42S01]: Base table or view already exists: 1050 Table
.
Proposed solution
The issue might stem from how Laravel handles migrations across multiple database connections in commands like migrate:fresh. Ideally, when specifying the --database flag, Laravel should restrict the migration process to that specific connection and not trigger migrations for any other database connection, such as conB.
Steps To Reproduce
Steps to reproduce: https://laracasts.com/discuss/channels/laravel/how-to-correctly-use-multiple-databases-on-the-same-connection-in-laravel
I followed this tutorial step by step