How to count a group by query in NodeJS Sequelize

后端 未结 2 1133
囚心锁ツ
囚心锁ツ 2021-02-12 14:57

In Rails I can perform a simple ORM query for the number of Likes a model has:

    @records = Model
        .select( \'model.*\' )
        .select( \'count(likes         


        
2条回答
  •  误落风尘
    2021-02-12 15:11

    This sequelize github issue looks totally like your case:

    User.findAll({
      attributes: ['User.*', 'Post.*', [sequelize.fn('COUNT', 'Post.id'), 'PostCount']],
      include: [Post]
    });
    

提交回复
热议问题