You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
findAndCountAll with order, limit and offset returns wrong result #4146
it works perfectly on findAll, as for now I'm using findAll and do pagination manually with lodash, but it's much slower compared to doing pagination with findAndCountAll
How does findAll work - I'd think it has the same problem of limiting first, and then applying order on that? Do you mean you remove the limit completely, load everyting and then limit in javscript?
Adding required: true to the include, could also fix the issue
Actually, I'm not sure how you would write a query for this - You need the join in order to do the order - but if you add the join, the limit won't work (since its a duplicating join, the limit will apply to the duplicated rows, not the parent rows)
@beeant I'm not entirely sure what you want to do honestly. Do you want to order the A rows by B.order? In that case, what happens when A is associated with several Bs, and each B has a different value for order - What should the ordering then be?
It probably works because we take the first A row we see in the result set (sorted according to the first B row that it was joined with) and merge the rest of the joined rows into that, so in that way we maintain sorted order. But this relies on an implementation detail in sequelize, and is not guaranteed to stay like that forever.
Can you show a full test case, with models, associations and test data that shows how findAndCount gives the wrong result? It should just call findAll with the options you gave
Hi @janmeier ,
I think I have an issue related with this, but the problem that I have is when I want to order by the first model. it's using the "order by" in the query that doesn't use the limit, so applies the order to the result of the query with limit.
This is shit of "sequelize" developers.
If you set option {subQuery: false} your aequelize shouldn't add any subqueries in you query =)
But it's not working.
Try set duplicating: false to your include, which duplicate
Reacted by Rafli
Reacted by Patrick Geyer, Christian Dannie Storgaard, Samuel Petroline, Alagu Selvan and Alan Yong
Reacted by Leandro Hoffmann, Woohyeok Jang, Arya Gamas Mahardika, Ahmad Alhasan, DEBRAIN Dev, Adam Burke, James, khairaf, Rik, Muhammad Uzair and 3 more
I am also running into this issue. Was a pretty massive waste of time for my team before we tracked this thread down. Adding {subQuery: false} Seems to be working.
why is this issue closed? Isn’t this a poor workaround for an obvious bug?
That looks like a different issue. What I am running into is that the sorting is applied after paging. I am expecting the sort to happen before paging.
For example: If I have it paged to 20 items I see the sort start over every 20 items. a - d on the first page and then more a - d on the second page. In order to find all the items starting with A I have to go through all the pages.
This is either a bug or a poor choice of performance over being practical.
Sounds like the ORDER BY is being generated outside the sub-select, which would cause it to run after pagination.
I haven't tested but I'll re-open. Can't promise when a fix will be available unfortunately :/
I am also running into this issue. Was a pretty massive waste of time for my team before we tracked this thread down. Adding {subQuery: false} Seems to be working. why is this issue closed? Isn’t this a poor workaround for an obvious bug?
I thought I was going crazy, but no, Sequelize was just trolling me with a weird "unknown column" error, from an open source project I am using, and all I did was add subQuery:false and the error is gone. But 'limit' does not work anymore. Great I love sequelize!
Activity
mickhansen commentedon Jul 19, 2015
You'll need to provide a lot more information for us to help you.
beeant commentedon Jul 29, 2015
for example:
when I do
the sorting doesn't seem to order the whole table (all pages), but only order the result of that page.
beeant commentedon Jul 29, 2015
seems to be related to this issue, but this issue is about findAll
#1412
janmeier commentedon Jul 29, 2015
Yeah, seems we need an order in the subquery
Dunno if we can add the order to the subquery in all cases though..
beeant commentedon Jul 29, 2015
it works perfectly on
findAll, as for now I'm usingfindAlland do pagination manually withlodash, but it's much slower compared to doing pagination withfindAndCountAlljanmeier commentedon Jul 29, 2015
How does findAll work - I'd think it has the same problem of limiting first, and then applying order on that? Do you mean you remove the limit completely, load everyting and then limit in javscript?
Adding
required: trueto the include, could also fix the issuebeeant commentedon Jul 29, 2015
oh yes, I removed all the limit and offset from
findAll, that's why it works.janmeier commentedon Jul 29, 2015
Actually, I'm not sure how you would write a query for this - You need the join in order to do the order - but if you add the join, the limit won't work (since its a duplicating join, the limit will apply to the duplicated rows, not the parent rows)
beeant commentedon Jul 29, 2015
I just tried to add
required: trueto the include, but doesn't solve the problemjanmeier commentedon Jul 29, 2015
@beeant I'm not entirely sure what you want to do honestly. Do you want to order the A rows by B.order? In that case, what happens when A is associated with several Bs, and each B has a different value for order - What should the ordering then be?
beeant commentedon Jul 29, 2015
Ya that's true, when we think in that sense that f hasMany associations. i want to sort it accoriding to the first associated modelB.
Even this is kind of weird, I wonder why it works with findAll
janmeier commentedon Jul 29, 2015
It probably works because we take the first A row we see in the result set (sorted according to the first B row that it was joined with) and merge the rest of the joined rows into that, so in that way we maintain sorted order. But this relies on an implementation detail in sequelize, and is not guaranteed to stay like that forever.
Can you show a full test case, with models, associations and test data that shows how findAndCount gives the wrong result? It should just call
findAllwith the options you gavepola88 commentedon Sep 1, 2015
Hi @janmeier ,
I think I have an issue related with this, but the problem that I have is when I want to order by the first model. it's using the "order by" in the query that doesn't use the limit, so applies the order to the result of the query with limit.
The query:
Correct me if I am wrong, but I think it should be:
Thanks,
PoLa
danconnell commentedon Feb 26, 2016
Is there any workaround to this? I'm having the same issue as @pola88.
15 remaining items
fgomezlo commentedon Dec 15, 2017
@mickhansen @danconnell "subQuery: false" it works for me. Thank you very much. but in Sequelize docs "subQuery" option doesn't appear :(
KobzarDenis commentedon Jul 4, 2018
This is shit of "sequelize" developers.
If you set option
{subQuery: false}your aequelize shouldn't add any subqueries in you query =)But it's not working.
Try set
duplicating: falseto your include, which duplicatedjustclean commentedon Jun 23, 2019
{subQuery: true} works but it gives duplicate record when association is N:M. even in V5 its not working for me.
AlaguSelvan commentedon Jul 8, 2020
Any Solution for this Issue, Still facing it.
jfbloom22 commentedon Feb 11, 2022
I am also running into this issue. Was a pretty massive waste of time for my team before we tracked this thread down. Adding {subQuery: false} Seems to be working.
why is this issue closed? Isn’t this a poor workaround for an obvious bug?
ephys commentedon Feb 11, 2022
Is the remaining issue the same as the one in #9940?
jfbloom22 commentedon Feb 11, 2022
That looks like a different issue. What I am running into is that the sorting is applied after paging. I am expecting the sort to happen before paging.
For example: If I have it paged to 20 items I see the sort start over every 20 items. a - d on the first page and then more a - d on the second page. In order to find all the items starting with A I have to go through all the pages.
This is either a bug or a poor choice of performance over being practical.
ephys commentedon Feb 11, 2022
Sounds like the ORDER BY is being generated outside the sub-select, which would cause it to run after pagination.
I haven't tested but I'll re-open. Can't promise when a fix will be available unfortunately :/
wenked commentedon Jan 29, 2023
gkp1 commentedon Mar 16, 2023
I thought I was going crazy, but no, Sequelize was just trolling me with a weird "unknown column" error, from an open source project I am using, and all I did was add subQuery:false and the error is gone. But 'limit' does not work anymore. Great I love sequelize!
shubhanshu-ssipmt commentedon Jan 11, 2024
i have tried using distinct true and duplicating false and seperate true in included models and its works fine for me