-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] fix: one of many subquery constraints #55168
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
76d099f
to
300f23a
Compare
300f23a
to
818ebee
Compare
Thanks! I just tested your branch on our project and query looks correct and I'm not running into db performance issues. |
c486d2f
to
8e4b5b9
Compare
8e4b5b9
to
f1d75eb
Compare
// We need to join subqueries that are not the innermost subquery | ||
// which is joined in the CanBeOneOfMany::ofMany method. | ||
if ($this->getOneOfManySubQuery() !== null) { | ||
$this->performJoin($query); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is admittedly not very pretty, but is necessary unless the Relation::addConstraints
signature is changed to allow passing the query instance in (see Additional Considerations section in PR description, and the first commit diff 818ebee for an example).
The root problem is that all subqueries need the join applied, but the innermost query is already being joined in the CanBeOneOfMany::ofMany
method (the addConstraints
call).
Hello!
This fixes the constraints on the OneOfMany subqueries.
Regression
The regression occurred because the
addConstraints
call was removed fromCanBeOneOfMany::ofMany
as it was adding duplicate join/where clauses to the HasOneThroughOfMany relations. Upon further inspection, this was because theHasOneOrMany::addConstraints
was using the query fromgetRelationQuery()
whereasHasOneOrManyThrough::addConstraints
was using the base$this->query
.Additional Considerations
Edit: I decided to forgo the signature BC, perhaps that could be looked at in a future upgrade.
The
CanBeOneOfMany::ofMany
callsaddConstraints
to add the constraints to the subquery, however, this only added the constraints to the first subquery (the innermost query assigned to$this->oneOfManySubQuery
. This means that queries with more than one aggregate will not have the constraints added to the other subqueries.The fix for this is to move the
addConstraints
call and pass in the subquery so that the constraints are added to every subquery.This can be seen in the following SQL (taken from the
testGlobalScopeIsNotAppliedWhenRelationIsDefinedWithoutGlobalScopeWithComplexQuery
test inDatabaseEloquentHasOneOfManyTest
):Closes #55155
CC: @BertvanHoekelen
Thanks!