[12.x] fix: one of many subquery constraints #55168
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!