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

Time:05-22

I Have the next attempt to do that, but is says, that "likesAmount" column doesn't exists. What should I do?

 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} ,
});

CodePudding user response:

You should use this literal in the where option like this:

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