Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
AND Condition on belongsToMany association SEQUELIZE

I'm using Sequelize and I have 2 models (Article and Tag) associated with belongsToMany through another model called ArticleTags.

I want to fetch all the Articles that have one Tag with id=1 AND one Tag with id=2.

I've tried multiple solutions, but it always fetches the Articles that have one Tag with id=1 OR one Tag with id=2.

This is the code I have as of now:

let articles = await Article.findAll({
    limit: limit, 
    offset: offset, 
    subQuery: false,
    where:[ {"$Tags.id$": {1,2} }]


    include:{
        model: Tag, 
        required: false
    }, 

});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change your where clause to this:

where: {
  "$Tags.id$": {
    [Op.and]: [1, 2]
  }
}
// SELECT * FROM articles WHERE $Tags.id$ = 1 AND $Tags.id$ = 2

This should work else have another look at the docs for applying where clauses

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda