Skip to content

findAndCountAll with order, limit and offset returns wrong result  #4146

Open

Description

@beeant

when I use findAndCountAll, it seems that the ordering is not the same on different pages (limit, offset)

Activity

  1. mickhansen commented on Jul 19, 2015

    @mickhansen
    Contributor

    You'll need to provide a lot more information for us to help you.

  2. beeant commented on Jul 29, 2015

    @beeant
    Author

    for example:

    modelA.hasMany(modelB);

    when I do

    modelA.findAndCountAll({
       include: [modelB],
       order: "`modelb.order`",
       limit: 10,
       offset: 10
    })

    the sorting doesn't seem to order the whole table (all pages), but only order the result of that page.

  3. beeant commented on Jul 29, 2015

    @beeant
    Author

    seems to be related to this issue, but this issue is about findAll
    #1412

  4. janmeier commented on Jul 29, 2015

    @janmeier
    Member

    Yeah, seems we need an order in the subquery

    SELECT `a`.*,
           `bs`.`id` AS `bs.id`,
           `bs`.`aId` AS `bs.aId`
    FROM
      (SELECT `a`.`id`
       FROM `as` AS `a` LIMIT 10,
                              10) AS `a`
    LEFT OUTER JOIN `bs` AS `bs` ON `a`.`id` = `bs`.`aId`
    ORDER BY `modelb.order`;

    Dunno if we can add the order to the subquery in all cases though..

  5. added
    type: bug
    on Jul 29, 2015
  6. beeant commented on Jul 29, 2015

    @beeant
    Author

    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

  7. janmeier commented on Jul 29, 2015

    @janmeier
    Member

    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

  8. beeant commented on Jul 29, 2015

    @beeant
    Author

    oh yes, I removed all the limit and offset from findAll, that's why it works.

  9. janmeier commented on Jul 29, 2015

    @janmeier
    Member

    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)

  10. beeant commented on Jul 29, 2015

    @beeant
    Author

    I just tried to add required: true to the include, but doesn't solve the problem

  11. janmeier commented on Jul 29, 2015

    @janmeier
    Member

    @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?

  12. beeant commented on Jul 29, 2015

    @beeant
    Author

    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

  13. janmeier commented on Jul 29, 2015

    @janmeier
    Member

    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

  14. pola88 commented on Sep 1, 2015

    @pola88
    Contributor

    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:

    SELECT `a`.*,
           `bs`.*
    FROM
      (SELECT `a`.`id`
       FROM `as` AS `a` LIMIT 10) AS `a`
    LEFT OUTER JOIN `bs` AS `bs` ON `a`.`id` = `bs`.`aId`
    ORDER BY `a`.`name` ASC;

    Correct me if I am wrong, but I think it should be:

    SELECT `a`.*,
           `bs`.*
    FROM
      (SELECT `a`.`id`
       FROM `as` AS `a` ORDER BY `a`.`name` ASC LIMIT 10) AS `a`
    LEFT OUTER JOIN `bs` AS `bs` ON `a`.`id` = `bs`.`aId`;

    Thanks,
    PoLa

  15. danconnell commented on Feb 26, 2016

    @danconnell
    Contributor

    Is there any workaround to this? I'm having the same issue as @pola88.

  16. 15 remaining items

  17. fgomezlo commented on Dec 15, 2017

    @fgomezlo

    @mickhansen @danconnell "subQuery: false" it works for me. Thank you very much. but in Sequelize docs "subQuery" option doesn't appear :(

  18. KobzarDenis commented on Jul 4, 2018

    @KobzarDenis

    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

  19. djustclean commented on Jun 23, 2019

    @djustclean

    {subQuery: true} works but it gives duplicate record when association is N:M. even in V5 its not working for me.

  20. AlaguSelvan commented on Jul 8, 2020

    @AlaguSelvan

    Any Solution for this Issue, Still facing it.

  21. jfbloom22 commented on Feb 11, 2022

    @jfbloom22

    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?

  22. ephys commented on Feb 11, 2022

    @ephys
    Member

    Is the remaining issue the same as the one in #9940?

  23. jfbloom22 commented on Feb 11, 2022

    @jfbloom22

    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.

  24. ephys commented on Feb 11, 2022

    @ephys
    Member

    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 :/

  25. reopened this on Feb 11, 2022
  26. wenked commented on Jan 29, 2023

    @wenked
  27. gkp1 commented on Mar 16, 2023

    @gkp1

    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!

  28. shubhanshu-ssipmt commented on Jan 11, 2024

    @shubhanshu-ssipmt

    i have tried using distinct true and duplicating false and seperate true in included models and its works fine for me

  29. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions