-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Nested preload with limit #5236
Comments
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days |
This is still an active issue for me |
@a631807682 thanks for the pointer to that PR! Does the new syntax allow a limit to be applied to the |
DB.Where("email = ?", "user").
Preload("Work").
Preload("Work.Project", func(tx *gorm.DB) *gorm.DB {
return tx.Limit(1).Order("projects.id DESC")
}).
Find(&rst).Error SQL is SELECT * FROM `projects` WHERE `projects`.`work_id` IN (1,2) ORDER BY projects.id DESC LIMIT 1
SELECT * FROM `works` WHERE `works`.`user_id` = 1
SELECT * FROM `users` WHERE email = "user" But it only has one Project in all Works. Try group by DB.Debug().
Where("email = ?", "user").
Preload("Work").
Preload("Work.Project", func(tx *gorm.DB) *gorm.DB {
return tx.Group("work_id")
}).
Find(&rst).Error
// SELECT * FROM `projects` WHERE `projects`.`work_id` IN (1,2) GROUP BY `work_id` |
It seems that we have no way to set the limit number of each subset in a query, @mnussbaum what is the raw sql you want? |
Your Question
I want to perform a limit on a nested preload to improve performance. The limit works on the total count on the children but I want the limit to work on each distinct owner.
The models are:
Query without Limit:
Result:
Query used to Limit Projects assocatied with Work to just one:
Result:
The Issue:
It limits total project fetched to just one instead one to each work.
Expected answer:
I seriously need help with this.
Thanks.
The text was updated successfully, but these errors were encountered: