Skip to content

Problems using findAndCount when looking for models with non-existent associations #5680

Closed

Description

@VividVisions

Hi!

I have the models Item and Category, which have a n:m relation. Now, I want to get all Items, where no Category has been assigned yet, which works when I use findAll but fails when I use findAndCount[All] (for pagination).

This works:

models.Item.findAll({
    attributes: ['name'],
    where: sequelize.literal('"categories"."id" IS NULL'),
    include: [models.Category]
});

This does not:

models.Item.findAndCountAll({
    attributes: ['name'],
    where: sequelize.literal('"categories"."id" IS NULL'),
    include: [models.Category]
});

I'm getting the following error:
Error: missing FROM-clause entry for table "categories"

In a similar setup, I'm getting the error SequelizeDatabaseError: column Item.val does not exist as soon as I change findAll to findAndCount.

I'm using:

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

Activity

  1. janmeier commented on Mar 31, 2016

    @janmeier
    Member

    Try

    where: { '$categories.id$': null }
  2. VividVisions commented on Mar 31, 2016

    @VividVisions
    Author

    @janmeier Thanks, but same effect.

  3. janmeier commented on Mar 31, 2016

    @janmeier
    Member

    The problem is that includes are removed from the count part - And when using literal or $$, we can't figure out that we should actually keep the include. Try adding required:true to the include, see #2663

  4. VividVisions commented on Mar 31, 2016

    @VividVisions
    Author

    required:true would return only Items with Categories in the first place.

  5. janmeier commented on Mar 31, 2016

    @janmeier
    Member

    Yes - unfortunately there is no good way to fix the issue right now - we need to be able to figure out that the $$ clause means we should keep the include, #4195 and #3095

  6. VividVisions commented on Mar 31, 2016

    @VividVisions
    Author

    That's unfortunate. :( I don't really get why the query has to be altered for the count in the first place but I guess, there's a reason. Making it optional, as mentioned in the linked issues, would be great.

    In the meantime, I tried a Postgres-specific workaround using COUNT(*) OVER():

    models.Item.findAll({
        attributes: [
             [sequelize.literal('COUNT(*) OVER()'), 'all'],
            'name'
        ],
        where: { '$categories.id$': null },
        include: [models.Category]
    });

    This works well until I add limit. Then the missing FROM-cause error occurs again.

  7. mickhansen commented on Mar 31, 2016

    @mickhansen
    Contributor

    @VividVisions When using postgres the window function is much better in any case.
    I'm not sure hwy it's not working for you here, can you try posting your SQL? (sounds like you might have some naming mismatch)

    We should really implement the window function in findAndCount for dialects that support it.

  8. mickhansen commented on Mar 31, 2016

    @mickhansen
    Contributor

    Hmm, except that PG does not support DISTINCT in window functions, so that won't work.

  9. VividVisions commented on Mar 31, 2016

    @VividVisions
    Author

    Hmm, except that PG does not support DISTINCT in window functions, so that won't work.

    Yes. But this wouldn't matter in my specific case or generally with models without any or only 1:1 or n:1 relations. (Right?)

    I'm not sure why it's not working for you here, can you try posting your SQL?

    Here's the generated (working) query without limit:

    SELECT "Item"."id", COUNT(*) OVER() AS "all", "Item"."name", "categories"."id" AS "categories.id", "categories"."name" AS "categories.name", "categories.CategoryRelation"."item_id" AS "categories.CategoryRelation.item_id", "categories.CategoryRelation"."category_id" AS "categories.CategoryRelation.category_id" FROM "items" AS "Item" LEFT OUTER JOIN ("category_relations" AS "categories.CategoryRelation" INNER JOIN "categories" AS "categories" ON "categories"."id" = "categories.CategoryRelation"."category_id") ON "Item"."id" = "categories.CategoryRelation"."item_id" WHERE ("categories"."id" IS NULL);

    And this one with limit fails :

    SELECT "Item".*, "categories"."id" AS "categories.id", "categories"."name" AS "categories.name", "categories.CategoryRelation"."item_id" AS "categories.CategoryRelation.item_id", "categories.CategoryRelation"."category_id" AS "categories.CategoryRelation.category_id" FROM (SELECT "Item"."id", COUNT(*) OVER() AS "all", "Item"."name" FROM "items" AS "Item" WHERE ("categories"."id" IS NULL) LIMIT 2) AS "Item" LEFT OUTER JOIN ("category_relations" AS "categories.CategoryRelation" INNER JOIN "categories" AS "categories" ON "categories"."id" = "categories.CategoryRelation"."category_id") ON "Item"."id" = "categories.CategoryRelation"."item_id";

    Am I interpreting this right: Adding limit changes the query to use a sub-query which doesn't know anything about categories at this point. Hence the error missing FROM-clause entry for table "categories".

  10. changed the title [-]findAndCount can't handle sequelize.literal[/-] [+]Problems using findAndCount when looking for models with non-existent associations[/+] on Apr 6, 2016
  11. VividVisions commented on Apr 6, 2016

    @VividVisions
    Author

    Just for the record: I solved this by denormalizing the number or categories per Item. I just added an integer field to Item and used Sequelize hooks to automatically update this field whenever categories get added or removed. Not very elegant, but it works.

    (I also changed the title of this issue because it has nothing to do with sequelize.literal.)

  12. Djalmar commented on May 16, 2016

    @Djalmar

    I'm having the same problem, the SQL code is:

    SELECT count(*) AS "count" FROM "public"."Item" AS "Item" WHERE "Category"."_id" IS NULL;

    There is any way to add the Category join in the count clause ??

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions