Simon Moulin
Simon Moulin

Reputation: 19

How to do this request using sequelize?

I would like to know the code to write to execute this request using sequelize:

select * from "Songs" 
where Id in (
    select "songId" from "Likes"
    where "userId"=1
)

Upvotes: 0

Views: 71

Answers (1)

Simon Moulin
Simon Moulin

Reputation: 19

I finaly found by myself how to do it, here is my code :

  const songs = await models.Songs.findAll({
    attributes: ['id', 'title', 'imageUrl', 'nbListening'],
    where: {
      id: {
        [Op.in]: sequelize.literal(
          `(select "songId" from "Likes" where "userId"=${req.user.id})`
        ),
      },
    },

Upvotes: 1

Related Questions