Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda