Add schema support for SQL Server driver #49976
-
Greetings everyone, I've been working with laravel for 3 months now (i love it) and eloquent has made my life so much easier with many SQL Server queries. But i've noticed that you can't do dynamic crossdatabase requests because SQL Server uses schemas and eloquent does not support it natively, here's an example of an insert into select between 2 databases :
the thrown error will be : Object name 'DATABASE2.Item' invalid. (Connexion: sqlsrv_database, SQL: insert into [current_item] ([id]) select [Id] from [DATABASE2].[Item]) because SQL Server uses schemas (in most cases 'dbo' by default) and needs to specify the schema before the table, the query would work if instead of DATABASE2.Item the target table from the other database was DATABASE2.schema.Item. You can actually set the schema for a database connexion in database.php but it doesn't do anything for sql server driver based connexions. The only workarounds i found are to either set a from with static database name and schema : This is my first contribution here, i hope this is a relevant topic |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Warning Eloquent does not currently support querying for relationship existence across databases. The relationships must exist within the same database.
framework/src/Illuminate/Database/Eloquent/Model.php Lines 561 to 574 in 9448e01 And there would need to be some kind of flag to determine of cross database joins are possible/wanted.
|
Beta Was this translation helpful? Give feedback.
-
Hello! Thank you for sharing your experience with Laravel and SQL Server. It's great to hear that Laravel has been helpful for you. Regarding your observation about dynamic cross-database requests, it sounds like you've encountered a limitation due to SQL Server's schema handling. Your suggested workarounds seem reasonable, and it's valuable to highlight this topic for discussion. Have you considered submitting a feature request or opening an issue on the Laravel GitHub repository to address this limitation? It could potentially benefit other users facing similar challenges. |
Beta Was this translation helpful? Give feedback.
-
We have better support for schema on sqlsrv and pgsql on Laravel 11, check #50019 |
Beta Was this translation helpful? Give feedback.
We have better support for schema on sqlsrv and pgsql on Laravel 11, check #50019