Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Contando entradas associadas com o Sequelize


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

Ou talvez seja necessário adicionar um group também:
Location.findAll({
    attributes: { 
        include: [[Sequelize.fn("COUNT", Sequelize.col("sensors.id")), "sensorCount"]] 
    },
    include: [{
        model: Sensor, attributes: []
    }],
    group: ['Location.id']
})