-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
FIX on CanBeOneOfMany trait giving erroneous results #46309
Conversation
After updating to Laravel 10.3, I get an error when using the has one of many feature. My relationship looks like: return $this->hasOne(Model::class)
->ofMany([
'status' => 'max',
'updated_at' => 'max',
]); And error is:
any clues on why could this be happening, or should I open a new issue? |
@dwightwatson I don't have time to fix RN so maybe it could be rollbacked ? The resolution of the problem would imply adding all aggregates "bubbling up" in the No idea how it passed the checks that operates tests with different databases, will investigate it. Sorry for the inconvenience. |
It is indeed strange that the tests passed. I created a PR reverting the change. I might have some time later in the week to code a solution using your hints. |
@strotgen |
This PR consists of 3 commits :
The origin of the problem is this :
take the relation
$this->hasOne(...)->ofMany(['updated_at' => 'max','created_at' => 'max']);
for some data, this would return an entry that actually has
updated_at
not corresponding to the max ofupdated_at
.This is caused by the fact that
CanBeOneOfMany
generated sql doesn't bubble up subqueries aggregates.To illustrate this, let's look at the sql generated by accessing the relation in the test I added :
In the second query, we select and then join on all previous aggregates done in the subqueries, which ensures that we are really computing the current aggregate on a set of rows that match all other aggregates.