Is it possible to use included custom attribute, created with sequelize.literal, in where statement

Viewed 84

Thats my attempt to filter records by custom field:

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']
    ]
  },
  limit,
  where: { likesAmount: 2 },
});

But it says that "likesAmount" column doesn't exists, is it possible to solve this problem? Would be grateful for the help.

0 Answers
Related