Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!