Skip to content

[9.x] Cross-database support for all relations #46675

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 21 commits into from

Conversation

bert-w
Copy link
Contributor

@bert-w bert-w commented Apr 3, 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)
  • There is still one issue with withCount on a morphTo() relation, where $users->query()->withCount('morphable')->get() results in a syntax error when no additional arguments are given to the relation (the SQL contains 'laravel_reserved_0'.'' because it is missing a key). The issue is solved though once you hardcode the relation as return $this->morphTo(null, null, null, 'id'). I excluded the test case from my tests it is an existing issue.

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

@bert-w bert-w changed the title [9.x] withCount doesn't use other database connection for joins when hasManyThrough [9.x] Various relationship methods don't work cross-database Apr 6, 2023
@GrahamCampbell
Copy link
Member

I don't think cross-database relationships is something that is meant to be supported by the framework.

@GrahamCampbell
Copy link
Member

In any case, if this is to be a new feature that is wanted, it would need to target 10.x, not 9.x.

@bert-w
Copy link
Contributor Author

bert-w commented Apr 8, 2023

Imo its definitely something that needs to be supported. Eloquent already has most of the infrastructure to deal with different connections. Besides that, this fix is relatively small since it only prepends database names when connections differ.

I would be fine with sending it to 10.x but I regard it as a bug since some of the functionality currently works and some doesn't.

@bert-w bert-w changed the title [9.x] Various relationship methods don't work cross-database [9.x] Cross-database support for all relations Apr 12, 2023
@bert-w bert-w closed this Apr 13, 2023
@bert-w
Copy link
Contributor Author

bert-w commented Apr 13, 2023

decided to send to 10.x instead #46769

@bert-w bert-w deleted the withcount-hasmanythrough branch April 13, 2023 20:31
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.

2 participants