findAll WHERE, using previously defined sequelize.fn attribute
See original GitHub issueHello,
I have two tables: products and brands and I am trying to fetch all brands where the associated product count is superior to 0.
So far I’ve been able to count the associated products and return that with the data as the attribute “product_count”, using a sequelize.fn:
models.Brand.findAll({
order: 'id ASC',
attributes: ['id', 'name', 'slug', [sequelize.fn('COUNT', sequelize.col('products.id')), 'product_count']],
include: [{model: models.Product, attributes:[]}],
group: ['brand.id']
})
This creates the following query (which works perfectly):
SELECT "brand"."id", "brand"."name", "brand"."slug", COUNT("products"."id") AS "product_count"
FROM "brands" AS "brand"
LEFT OUTER JOIN "products" AS "products" ON "brand"."id" = "products"."brand_id"
GROUP BY "brand"."id"
ORDER BY id ASC;
Now, instead of returning all of the brands, I’d like to only return brands where the product count is superior to 0. I tried this:
models.Brand.findAll({
order: 'id ASC',
attributes: ['id', 'name', 'slug', [sequelize.fn('COUNT', sequelize.col('products.id')), 'product_count']],
include: [{model: models.Product, attributes:[]}],
group: ['brand.id'],
where: {
'product_count': {
$gt: 0
}
}
})
But it spits out an error:
{"error":{"name":"SequelizeDatabaseError","message":"column brand.product_count does not exist"}}
With the query being:
SELECT "brand"."id", "brand"."name", "brand"."slug", COUNT("products"."id") AS "product_count"
FROM "brands" AS "brand"
LEFT OUTER JOIN "products" AS "products" ON "brand"."id" = "products"."brand_id"
WHERE "brand"."product_count" > 0
GROUP BY "brand"."id"
ORDER BY id ASC;
Is it not possible to reference an attribute previously defined with sequelize.fn in the where? Is there a solution that I am not aware of?
Thank you
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Model Querying - Basics - Sequelize
You can use sequelize.fn to do aggregations: Model.findAll({ attributes: [ 'foo', [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats'],
Read more >Can't use attributes with sequelize.fn inside include block of ...
My main question here is can we or can't we use sequelize.fn inside some include block of some query like model.findAll(). javascript ·...
Read more >The Comprehensive Sequelize Cheatsheet
To set up a basic model with only attributes and their datatypes. const ModelName = sequelize.define("tablename", { // s will be appended ...
Read more >Model | Sequelize
Initialize a model, representing a table in the DB, with attributes and options. The table columns are defined by the hash that is...
Read more >How to use the sequelize.col function in sequelize - Snyk
blocksModel.findAll({ attributes: [ [sequelize.fn('MAX', sequelize.col('height')), ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
try
sequelize.where('product_count', {$gt: 0})
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 🙂