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
Problems using findAndCount when looking for models with non-existent associations #5680
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.
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
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
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():
@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.
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".
changed the title [-]findAndCount can't handle sequelize.literal[/-][+]Problems using findAndCount when looking for models with non-existent associations[/+]on Apr 6, 2016
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.)
Activity
janmeier commentedon Mar 31, 2016
Try
VividVisions commentedon Mar 31, 2016
@janmeier Thanks, but same effect.
janmeier commentedon Mar 31, 2016
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 addingrequired:trueto the include, see #2663VividVisions commentedon Mar 31, 2016
required:truewould return only Items with Categories in the first place.janmeier commentedon Mar 31, 2016
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
VividVisions commentedon Mar 31, 2016
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():This works well until I add
limit. Then themissing FROM-causeerror occurs again.mickhansen commentedon Mar 31, 2016
@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.
mickhansen commentedon Mar 31, 2016
Hmm, except that PG does not support DISTINCT in window functions, so that won't work.
VividVisions commentedon Mar 31, 2016
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?)
Here's the generated (working) query without
limit:And this one with
limitfails :Am I interpreting this right: Adding
limitchanges the query to use a sub-query which doesn't know anything about categories at this point. Hence the errormissing FROM-clause entry for table "categories".[-]findAndCount can't handle sequelize.literal[/-][+]Problems using findAndCount when looking for models with non-existent associations[/+]VividVisions commentedon Apr 6, 2016
Just for the record: I solved this by denormalizing the number or categories per
Item. I just added anintegerfield toItemand 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.)Djalmar commentedon May 16, 2016
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 ??