question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findAll WHERE, using previously defined sequelize.fn attribute

See original GitHub issue

Hello,

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:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
felixfbeckercommented, Sep 21, 2016

try sequelize.where('product_count', {$gt: 0})

0reactions
stale[bot]commented, Jun 29, 2017

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 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found