-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Issues with php artisan migrate:fresh affecting multiple database connections #55194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this topic https://stackoverflow.com/questions/72151051/run-migration-for-different-database-connections is also connected with this bug |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
@crynobone I don't think I need help, it's bug |
I think you just use the migration system wrong, it is not expected to use Schema::connection in the migration files. Write the migrations from your secondary database in another directory or subdirectory, then call something like |
@NiroDeveloper Thanks for your suggestion! I really appreciate the thought, but in our setup we’re using the I actually love the cleanness of the approach in the Laracasts article. Using The real pain point is It just feels inconsistent that |
The problem is that
Personally i fixed this problem by overwriting the command so it calls migrate:fresh multiple times for every database i have. Also do not forget to set $connectionsToTransact in your test case, if you use a database testing trait. |
@NiroDeveloper thank you for your detailed explanation!
I see now that the real blocker is db:wipe (and thus migrate:fresh) operates only on the default connection, not that the
I’ve done the same—overriding Here are three ideas I haven’t seen fully explored yet:
You also mentioned:
Could you expand on that? Are you suggesting wrapping each DB connection in a transaction (instead of rebuilding schemas) and rolling them back after tests? I’ve been using Thanks again for your insights—this feels more like a small enhancement or bugfix than a “help wanted” question @crynobone. If we can agree on an approach, I’d be happy to draft a PoC or open a PR. |
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:
or just
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
The text was updated successfully, but these errors were encountered: