-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Database Connection Prefixes behave unexpectedly #26589
Comments
I kind of feel that a raw query has no way to know to use a prefix. You could write anything in that raw query, so it would be your responsibility to handle any prefixing. Seems kind of expected to me...? |
@laurencei is right. For raw queries this is the expected way. There's no way to format the queries with the prefix. Raw queries are meant to just accept the input provided and not modify it. |
@driesvints , @laurencei absolutely correct. But in this case, database prefixing is failing to provide connection-agnostic code. If this was an application, that's okay, but as a framework, it's providing something unexpected and undocumented, which leads to unexpected application faults. |
Only because of your use of “DB::raw()” to do connection related queries.
The whole point of DB::raw() is to allow you to type anything you want
n there, and Laravel won’t touch it.
Connection/DB agnostic support only applies to the normal queries,
which would all work for your examples above.
|
But you'd need to have knowledge of internals to know which of the documented DB methods use In fact, after further inspection |
Personally, I'd be happy if the action for this ticket was: "update the documentation with a disclaimer that prefixes don't work for Raw SQL Queries." |
Description:
Database connection prefixes behave inconsistently and unexpectedly.
Steps To Reproduce:
Configure database in config/database.php thusly:
Create a basic migration file:
If we generate the table, these connections perform as expected.
Two tables, a_posts and b_posts are created.
If we an Eloquent Post model, or use the Query Builder syntax, they perform as expected.
However for more 'raw' queries, the prefix fails to fire:
What this means is, database connection prefixes are inconsistent, and mistakenly suggest that one can be swapped out for the another, and that code can run without knowing what connection-specific name of each table is.
Also, it seems like factory methods for unit testing use the latter format to construct queries.
Why would you want to use connections in this way? One use case is retrofitting an app to a multi-site Wordpress database. Instead of having an AlphaPost and BetaPost and so on for every single site, we want to build a single Post object to generically handle an unknown, application-specific number of post tables. Using connections to separate the different sections of the database means we can use
connection('alpha')
orPost::on('alpha')
to ensure our Post is accessing the right set of database tables.The text was updated successfully, but these errors were encountered: