node.js - How to count a group by query in NodeJS Sequelize -
in rails can perform simple orm query number of likes model has:
@records = model .select( 'model.*' ) .select( 'count(likes.*) likes_count' ) .joins( 'left join likes on model.id = likes.model_id' ) .group( 'model.id' )
this generates query:
select models.*, count(likes.*) likes_count "models" join likes on models.id = likes.model_id group models.id
in node sequelize, attempt @ doing similar fails:
return model.findall({ group: [ '"model".id' ], attributes: ['id', [sequelize.fn('count', sequelize.col('"likes".id')), 'likes_count']], include: [{ attributes: [], model: }], });
this generates query:
select model.id, count(likes.id) likes_count, likes.id likes.id # bad! models model left outer join likes likes on model.id = likes.model_id group model.id;
which generates error:
column "likes.id" must appear in group clause or used in aggregate function
it's erroneously selecting likes.id, , have no idea why, nor how rid of it.
this sequelize github issue looks totally case:
user.findall({ attributes: ['user.*', 'post.*', [sequelize.fn('count', 'post.id'), 'postcount']], include: [post] }
Comments
Post a Comment