Skip to content

findAll returns wrong number of instances when using LIMIT and filtered many-to-many association #5715

Closed

Description

@VividVisions

When using LIMIT and a filtered many-to-many association, findAll returns the wrong number of instances. In my case, it was always one less than expected.

Item.findAll({
   limit: 2,
   include: {
      model: Category,
      required: true,
      where: {
         name: {
            $like: '%1'
         }
      }
   }
});

I've written a simple app to check if I can reproduce the behaviour, which I copied to this gist. (Sorry it's not a proper test.) This code should return two items, but it returns only one.

I'm using:

  • Sequelize v3.21.0
  • Dialect: postgres
  • PostgreSQL v9.4.4.1

Activity

  1. janmeier commented on Apr 7, 2016

    @janmeier
    Member

    What does the sql look like?

  2. VividVisions commented on Apr 7, 2016

    @VividVisions
    Author

    This is the generated SQL:

    SELECT "items".*,
           "categories"."id" AS "categories.id",
           "categories"."name" AS "categories.name",
           "categories.relation"."categoryId" AS "categories.relation.categoryId",
           "categories.relation"."itemId" AS "categories.relation.itemId"
    FROM
      (SELECT "items"."id",
              "items"."name"
       FROM "items" AS "items"
       WHERE
           (SELECT "relation"."categoryId"
            FROM "relations" AS "relation"
            INNER JOIN "categories" AS "category" ON "relation"."categoryId" = "category"."id"
            WHERE ("items"."id" = "relation"."itemId") LIMIT 1) IS NOT NULL LIMIT 2) AS "items"
    INNER JOIN ("relations" AS "categories.relation"
    INNER JOIN "categories" AS "categories" ON "categories"."id" = "categories.relation"."categoryId") ON "items"."id" = "categories.relation"."itemId"
    AND "categories"."name" LIKE '%1';
  3. added
    type: bug
    on Apr 8, 2016
  4. mickhansen commented on Apr 8, 2016

    @mickhansen
    Contributor

    The include.where statement is not used in the subquery filter on the include. So the subquery will return two rows but they won't all have the correct categories.name and then be filtered further.

  5. Djalmar commented on Apr 11, 2016

    @Djalmar

    Same problem here, any workaround ?

  6. davidninam commented on Apr 12, 2016

    @davidninam

    Any news, about this problem?

  7. Alexsey commented on Jun 9, 2017

    @Alexsey
    Contributor

    My issue #7344 is looks like the same. #4716 is also looks like the same and there is a workaround - add subQuery: false

  8. stale commented on Aug 12, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  9. Djalmar commented on Sep 4, 2017

    @Djalmar

    is still a issue

  10. SirWojtek commented on Jan 9, 2018

    @SirWojtek

    I found another workaround working for me. I've just added distinct: true to my findAndCountAll query.

  11. mllumiquinga194 commented on Aug 17, 2019

    @mllumiquinga194

    Any news, about this problem?

    subQuery: false, 16-08-2019

  12. papb commented on Aug 17, 2019

    @papb
    Member

    I am confused, is this still an issue with the latest Sequelize? If yes, can someone please post an SSCCE (also known as MCVE/reprex)? Once it's done I will reopen this.

  13. Drilonpacarizi commented on Nov 21, 2020

    @Drilonpacarizi
       limit: 2,
       include: {
          model: Category,
          required: true,
          separate: true       <--------------- add this if Item associate with  Category is ,1: many or many: many
          where: {
             name: {
                $like: '%1'
             }
          }
       },
      subQuery: false,           <-------- Add this
       distinct: true,               <--------
    });```
    
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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions