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
Order and Limit by included model #11288
Comments
I was able to reproduce in the latest version. But if this would be "fixed", it would be a breaking change... I can also see usage for the current behavior, no? Perhaps this should be considered a feature instead of a bug? |
@papb I can see it could be a breaking change. I'd be super happy to simply be told I'm approaching it wrong. The essence of the problem is how to I get a limited set of model A, sorted by a property of associated model B. |
@mmmmmrob same thing happened to me, was trying to debug and find what I was doing wrong, until I realised it doesn't work at all. Would love to create a feature PR. |
Same problem here. |
@securedeveloper If you are willing to submit a PR, that would be great!! Even better if you make it a feature rather than a change. |
Is there really no way to model and query this scenario using the current sequelize features? It seems like such a common thing to need that I assumed my modelling/querying was the problem and someone would say "oh, you need to query like this..." I mean, I'm surprised because sequelize is so complete, and "like this" is the answer I've got to every previous question. |
@mmmmmrob Good point. Perhaps there is a way to do it already... Although I am a maintainer, there are many things in Sequelize I don't know (yet)! @sushantdhiman can you take a look? Have you tried Stack Overflow? Maybe you can ask and add a link here. Or try Slack as well... |
@papb I ended up asking here because I'd asked several times over a few weeks on slack and got no reply. I think the question is a bit too high-level for slack. |
@mmmmmrob I see. Let's wait to see what sushantdhiman has to say about it. |
I workaround with |
An interesting workaround is to inject a
Maybe a nicer way to do this would be inject a |
Another way is set duplicating false every include then a normal order. It will join everything first then order maybe impact the performance in some case. |
But we still need order in include |
When I order with literal it's work but when I use [Model,'DESC'] it's put order only the outside |
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
I spent hours on this. Workaround that worked for me is,
This is three nested query. My query looked like |
I tried both Using limit, this did not work: order: [
[{ model: AdminNotice, as: 'adminNotice' }, 'sendOn', 'DESC'],
],
limit: 30,
offset: 0 translated to sql: SELECT `Notice`.* FROM (
SELECT
`Notice`.`id`,
`adminNotice`.`noticeId` AS `adminNotice.noticeId`,
`adminNotice`.`sendOn` AS `adminNotice.sendOn`
FROM `Notices` AS `Notice`
INNER JOIN `AdminNotices` AS `adminNotice` ON `Notice`.`id` = `adminNotice`.`noticeId`
LIMIT 0, 30
) AS `Notice`
ORDER BY `adminNotice`.`sendOn` DESC; with error: Commenting out the limit made it succeed, turning it into SELECT
`Notice`.`id`,
`adminCalendarNotice`.`noticeId` AS `adminCalendarNotice.noticeId`,
`adminCalendarNotice`.`sendOn` AS `adminCalendarNotice.sendOn`
FROM `Notices` AS `Notice`
INNER JOIN `AdminCalendarNotices` AS `adminCalendarNotice` ON `Notice`.`id` = `adminCalendarNotice`.`noticeId`
ORDER BY `adminCalendarNotice`.`sendOn` DESC
LIMIT 0, 10000000000000; But needing the limit, I found that this would work: order: [
[sequelize.literal('`adminNotice.sendOn` DESC')],
] turning the sql into: SELECT `Notice`.*
FROM (
SELECT `Notice`.`id`, `adminNotice`.`noticeId` AS `adminNotice.noticeId`, `adminNotice`.`sendOn` AS `adminNotice.sendOn`
FROM `Notices` AS `Notice`
INNER JOIN `AdminNotices` AS `adminNotice` ON `Notice`.`id` = `adminNotice`.`noticeId`
ORDER BY `adminNotice.sendOn` DESC LIMIT 0, 30
) AS `Notice`
ORDER BY `adminNotice.sendOn` DESC; |
you can try using seperate: true in included model |
try using seperate: true |
Based on this limit : limit,
subQuery:false Somehow avoiding nested query fixed the problem for me. |
It works but it reduces the results and not give required limited counted results |
What are you doing?
Ordering or limiting by an associated model. Example here would be:
find the 10 brands with the best reviewed stores
Other examples might be:
Lots of cases where you want to find a distinct set of a model, ordered by a property of an associated model.
To Reproduce
Steps to reproduce the behavior:
What do you expect to happen?
Returns brands 21..12
What is actually happening?
Returns brands 9..0
Environment
Dialect:
Dialect library version: XXX
Database version: XXX
Sequelize version: 4.35.2
Node Version: 12.6.0
OS: Linux
If TypeScript related: TypeScript version: XXX
Tested with latest release:
The text was updated successfully, but these errors were encountered: