Sequelize:如何找到点赞数等于 2 的所有行

Sequelize: How to find all rows where amount of likes are equal to 2

我下一次尝试这样做,但据说“likesAmount”列不存在。我该怎么办?

 let pictures = await models.Picture.findAll({
  attributes: {
    include: [
      [sequelize.literal('(SELECT COUNT(*) FROM "pictureLikes" WHERE "pictureLikes"."pictureId"=picture.id)'), 'likesAmount'],
      [sequelize.literal('(SELECT COUNT(*) FROM comments WHERE comments."pictureId"=picture.id)'), 'commentsAmount']
    ]
  },
  where:{likesAmount: 2} ,
});

您应该像这样在 where 选项中使用此文字:

where: Sequelize.where(sequelize.literal('(SELECT COUNT(*) FROM "pictureLikes" WHERE "pictureLikes"."pictureId"=picture.id)'), Op.eq, '2');