Skip to content

FindAll and FindAllAndCount with Includes and Limits - [fix proposal for incorrect behavior] #10812

Open

Description

@radoslavpetranov

Right now, when you execute a findAll or a findAllAndCount call with includes and add a limit to your options, sequelize will produce something along the lines of

SELECT ... cols ... 
FROM (SELECT ... cols ... FROM `table` AS `table` LIMIT 0, 10) AS `table`
LEFT OUTER JOIN `table2` AS `table2` ON `table`.`col` = `table2`.`col` 
ORDER BY `table2`.`col` DESC;

which is wrong in every single pagination scenario where some sort of filtering and/or sorting needs to be done on the association (i.e. table2). This is not some obscure edge case but a trivial pagination + sorting case. The correct query for any pagination scenario would be

SELECT ... cols ... 
FROM (SELECT ... cols ... FROM `table` AS `table`) AS `table`
LEFT OUTER JOIN `table2` AS `table2` ON `table`.`col` = `table2`.`col` 
ORDER BY `table2`.`col` DESC  LIMIT 0, 10;

I came across this 2 years old thread which seems to have been closed without a solution so I took a quick look at the sequelize source code and while I can't (and don't want to) propose an in-depth solution to the query generation logic, I think you can give us a way to work around this problem without major code changes and without forcing us to rely on ugly pure SQL query fixes.

If you take a look at https://github.com/sequelize/sequelize/blob/master/lib/dialects/abstract/query-generator.js#L1329

const limitOrder = this.addLimitAndOffset(options, mainTable.model);
if (limitOrder && !options.groupedLimit) {
	if (subQuery) {
	subQueryItems.push(limitOrder);
	} else {
	mainQueryItems.push(limitOrder);
	}
}

if the findAll or findAllAndCount query has any includes, the subQuery variable will be true and the limit will be added to subQueryItems and thus applied to the subquery.

A very simple workaround would be to just change that if-statement to

if (subQuery && !options.isGlobalLimit) {
	subQueryItems.push(limitOrder);
} else {
	mainQueryItems.push(limitOrder);
}

so that we can simply change our findAll call from

modelA.findAll({ where: {...}, include: [...], order: [ ... ], limit: 10 })

to

modelA.findAll({ where: {...}, include: [...], order: [ ... ], limit: 10, isGlobalLimit: true })

and we would get the correct pagination query with the limit being applied to the main query instead of the subquery.

It's not a solution to the logical problem but at least it gives us a way to implement correct sorting/filtering behavior using sequelize without any in-depth changes and without introducing any bugs.

Do you think this change could make it into the next release?

Activity

  1. Serginio06 commented on May 8, 2019

    @Serginio06

    Faced with the same issue. subQuery: false works for me

  2. VishalMSoni commented on May 13, 2019

    @VishalMSoni

    subQuery: false is working but it is giving distinct records only by default it is applying distinct true.

  3. krazi3 commented on Jun 25, 2019

    @krazi3
    Contributor

    @radoslavpetranov If we remove limit from the sub-query this will cause the sub-query to return all the rows in table1 which in return will slow the query down when you have thousands of results in table1. A more better solution (what i think) will be to add a join in the sub-query with table2 and limit / sort with the included model.

  4. radoslavpetranov commented on Jun 25, 2019

    @radoslavpetranov
    Author

    @krazi3 you don't remove the limit - you move it to the upper level query. That's exactly what you want to do when you want to do a join correctly and then limit the results of that join.

  5. JamesMGreene commented on Sep 26, 2019

    @JamesMGreene

    Related: #10557

  6. added
    type: bug
    type: feature
    on Oct 23, 2019
  7. harshmalvi97 commented on Jul 20, 2020

    @harshmalvi97

    Right now, when you execute a findAll or a findAllAndCount call with includes and add a limit to your options, sequelize will produce something along the lines of

    SELECT ... cols ... 
    FROM (SELECT ... cols ... FROM `table` AS `table` LIMIT 0, 10) AS `table`
    LEFT OUTER JOIN `table2` AS `table2` ON `table`.`col` = `table2`.`col` 
    ORDER BY `table2`.`col` DESC;
    

    which is wrong in every single pagination scenario where some sort of filtering and/or sorting needs to be done on the association (i.e. table2). This is not some obscure edge case but a trivial pagination + sorting case. The correct query for any pagination scenario would be

    SELECT ... cols ... 
    FROM (SELECT ... cols ... FROM `table` AS `table`) AS `table`
    LEFT OUTER JOIN `table2` AS `table2` ON `table`.`col` = `table2`.`col` 
    ORDER BY `table2`.`col` DESC  LIMIT 0, 10;
    

    I came across this 2 years old thread which seems to have been closed without a solution so I took a quick look at the sequelize source code and while I can't (and don't want to) propose an in-depth solution to the query generation logic, I think you can give us a way to work around this problem without major code changes and without forcing us to rely on ugly pure SQL query fixes.

    If you take a look at https://github.com/sequelize/sequelize/blob/master/lib/dialects/abstract/query-generator.js#L1329

    const limitOrder = this.addLimitAndOffset(options, mainTable.model);
    if (limitOrder && !options.groupedLimit) {
    	if (subQuery) {
    	subQueryItems.push(limitOrder);
    	} else {
    	mainQueryItems.push(limitOrder);
    	}
    }
    

    if the findAll or findAllAndCount query has any includes, the subQuery variable will be true and the limit will be added to subQueryItems and thus applied to the subquery.

    A very simple workaround would be to just change that if-statement to

    if (subQuery && !options.isGlobalLimit) {
    	subQueryItems.push(limitOrder);
    } else {
    	mainQueryItems.push(limitOrder);
    }
    

    so that we can simply change our findAll call from

    modelA.findAll({ where: {...}, include: [...], order: [ ... ], limit: 10 })
    

    to

    modelA.findAll({ where: {...}, include: [...], order: [ ... ], limit: 10, isGlobalLimit: true })
    

    and we would get the correct pagination query with the limit being applied to the main query instead of the subquery.

    It's not a solution to the logical problem but at least it gives us a way to implement correct sorting/filtering behavior using sequelize without any in-depth changes and without introducing any bugs.

    Do you think this change could make it into the next release?

    Hey @radoslavpetranov can you please give example for better understanding?
    const limitOrder = this.addLimitAndOffset(options, mainTable.model);
    if (limitOrder && !options.groupedLimit) {
    if (subQuery) {
    subQueryItems.push(limitOrder);
    } else {
    mainQueryItems.push(limitOrder);
    }
    }

  8. derMart commented on Nov 23, 2020

    @derMart

    same issue here, adding subQuery: false to the first argument of the findAll call is a workaround for the issue for me too.
    Also, subQuery and distinct options are not documented, which needs to be fixed.

  9. github-actions commented on Nov 13, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  10. added theissue type on Mar 9, 2025
  11. removed
    type: bug
    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: feature

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions