Counting associated entries with Sequelize

前端 未结 3 1675
挽巷
挽巷 2020-12-08 07:38

I have two tables, locations and sensors. Each entry in sensors has a foreign key pointing to locations. Using Sequelize,

相关标签:
3条回答
  • 2020-12-08 08:04
    Location.findAll({
            attributes: { 
                include: [[Sequelize.fn("COUNT", Sequelize.col("sensors.id")), "sensorCount"]] 
            },
            include: [{
                model: Sensor, attributes: []
            }]
        });
    

    and it works. but when i add "limit", i got error: sensors undefined

    0 讨论(0)
  • 2020-12-08 08:14

    For Counting associated entries with Sequelize

    Location.findAll({
        attributes: { 
            include: [[Sequelize.fn('COUNT', Sequelize.col('sensors.location')), 'sensorCounts']] 
        }, // Sequelize.col() should contain a attribute which is referenced with parent table and whose rows needs to be counted
        include: [{
            model: Sensor, attributes: []
        }],
        group: ['sensors.location'] // groupBy is necessary else it will generate only 1 record with all rows count
    })
    

    Note :

    Some how, this query generates a error like sensors.location is not exists in field list. This occur because of subQuery which is formed by above sequelize query.

    So solution for this is to provide subQuery: false like example

    Location.findAll({
            subQuery: false,
            attributes: { 
                include: [[Sequelize.fn('COUNT', Sequelize.col('sensors.location')), 'sensorCounts']] 
            },
            include: [{
                model: Sensor, attributes: []
            }],
            group: ['sensors.location']
        })
    

    Note: **Sometime this could also generate a error bcz of mysql configuration which by default contains only-full-group-by in sqlMode, which needs to be removed for proper working.

    The error will look like this..**

    Error : Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db.table.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

    So to resolve this error follow this answer

    SELECT list is not in GROUP BY clause and contains nonaggregated column .... incompatible with sql_mode=only_full_group_by

    Now this will successfully generate all associated counts

    Hope this will help you or somebody else!

    0 讨论(0)
  • 2020-12-08 08:17

    Use findAll() with include() and sequelize.fn() for the COUNT:

    Location.findAll({
        attributes: { 
            include: [[Sequelize.fn("COUNT", Sequelize.col("sensors.id")), "sensorCount"]] 
        },
        include: [{
            model: Sensor, attributes: []
        }]
    });
    

    Or, you may need to add a group as well:

    Location.findAll({
        attributes: { 
            include: [[Sequelize.fn("COUNT", Sequelize.col("sensors.id")), "sensorCount"]] 
        },
        include: [{
            model: Sensor, attributes: []
        }],
        group: ['Location.id']
    })
    
    0 讨论(0)
提交回复
热议问题