Skip to content

[10.x] Cross-database support for all relations #46769

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

Closed
wants to merge 3 commits into from

Conversation

bert-w
Copy link
Contributor

@bert-w bert-w commented Apr 13, 2023

Related to #23042

Issue

Various relationship methods do not work with cross database relations including withCount and whereHas. SQL errors were thrown because the database name was not prefixed in all cases. Some work has been done previously to accomplish this, however it wasn't tested thoroughly and several cases were left unconsidered (like joins and specific relation types).

Requirements

  • Any MorphToMany/BelongsToMany relations are required to have an intermediary Eloquent model defined like so, because the connection from the intermediary table cant be resolved otherwise:
public function taggables()
{
    return $this->morphToMany(...)->using(Taggable::class);
}
  • The rest of the relations work out of the box and any joins/froms that happen on a different connection automatically have their database name prefixed.

Task status

  • Test all relationship methods (only correct SQL execution is tested)

Support status

  • MySQL 5.7
  • MySQL 8.0
  • MariaDB 10.0
  • SQL Server 2019 (FIXED prefixing dbo was required. A custom config override 'schema' has been added in the database configuration, similar to Postgres)
  • NOT SUPPORTED: PostgreSQL 14 (it requires additional pgsql extensions dblink and/or postgres_fdw)
  • NOT SUPPORTED: SQLite

@laravel laravel deleted a comment from Mastacow Apr 24, 2023
@bert-w bert-w marked this pull request as ready for review April 24, 2023 20:14
@taylorotwell
Copy link
Member

I don't make it a point or goal to support relationships across databases. I just don't think it's a good idea.

@bert-w
Copy link
Contributor Author

bert-w commented Apr 24, 2023

@taylorotwell then why support it "somewhat" using the existing Illuminate\Database\Query::prependDatabaseNameIfCrossDatabaseQuery()?

There are countless usecases and reasons to support this (think of tenancy databases or a separate administration database that links to the main one). But most of all it is yet another reason for people to pick Laravel for their application.

@crissi
Copy link
Contributor

crissi commented Apr 28, 2023

@bert-w let me know if you ever make a package for this

@bert-w
Copy link
Contributor Author

bert-w commented Apr 28, 2023

@crissi I've monkeypatched this myself by overriding a few composer files (you can define certain files in your composer.json to take precedence over the ones in Composer, then just add your own overrides in something like app\Overrides\Builder.php).

I dont think its practical to release this as a package since its not pretty to extend onto the existing Laravel classes and have them loaded instead of the default.

@nicholasbrantley
Copy link
Contributor

@taylorotwell then why support it "somewhat" using the existing Illuminate\Database\Query::prependDatabaseNameIfCrossDatabaseQuery()?

There are countless usecases and reasons to support this (think of tenancy databases or a separate administration database that links to the main one). But most of all it is yet another reason for people to pick Laravel for their application.

the separate admin database is the use case I need this functionality for, and I'm requesting this be reconsidered. After using other built in cross database functionality like being able to set relations across models, the lack of being able to query with that relation is disappointing.

@byErikas
Copy link

Sorry for necroing, but I'm requesting this to be re-reviewed, and reconsidered. The support for this is already half-way existent, and would greatly assist many multi-db laravel users

@harryqt
Copy link
Contributor

harryqt commented May 27, 2025

Laravel 12 (#54274) supports cross-database queries natively. 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants