-
Notifications
You must be signed in to change notification settings - Fork 11.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
New limit behavior breaking database queries #50602
Comments
As a solution right now, it's possible to load data as plain query: $model
->load(['transactions' => fn ($q) => Transaction::scopeReferralGroups($q->getQuery(), 3)]) |
Hi @punyflash, You can use $model->setRelation(
'transactions',
Transaction::scopeReferralGroups($model->transactions()->getQuery(), 3)->get()
); |
@staudenmeir, yeah, I agree, wasn't paying attention that result is not set, your example seems more reasonable. Though, the issue is still there: adding the limit statement to the relationship query adds a window function to SQL query where it should not be. I can simplify my code example if $model->load([
'transactions' => fn ($q) => $q->select(
DB::raw('DATE(completed_at) as date'),
DB::raw('COUNT(*) as count'),
DB::raw('SUM(credits_amount) as credits_total'),
)
->where('is_referral', true)
->orderBy('date', 'desc')
->groupBy('date')
->limit(3) // breaks with limit
]); |
why not? $model->load(['transactions' => fn ($q) => $q->referralGroups(3)]); |
The result is not set because your query is not a valid relationship query. It doesn't select actual transactions but custom aggregate rows. The result can only be set if you select the Laravel 11's limit implementation "breaks" your query, but the query itself is already invalid in combination with |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Laravel Version
11.0.7
PHP Version
8.3.3
Database Driver & Version
PgSQL 15.6
Description
I've used this query to display total amount of transactions on a day basis:
This works correctly on Laravel 10.x or without
limit
statement, but now fails with error:Right now, adding a
limit
clause produces a database query wrapped in window function:However expected query is:
I believe this is something to do with #49695. I think this should not be implemented as default behavior, but rather added with condition, for example like
limit(3, window: true)
Steps To Reproduce
Add
limit
clause to any ordered group by query onHasMany
relationship instanceThe text was updated successfully, but these errors were encountered: