aName
aName

Reputation: 3043

How to filter with indirect association

I'm new to Sequelize, I have the following schema (it's an example) : enter image description here

How can I use sequelize to get All users that live in a given city,
I looked in Sequelize doc, but I don't figure out how to make indirect association filter.

Upvotes: 0

Views: 58

Answers (1)

Ellebkey
Ellebkey

Reputation: 2301

You have to add an include of the model that has the association on each step.

const users = await db.User.findAll({
  include: [{ 
    model: db.Country, 
    as: 'Country', 
    include: [{
      model: db.City,
      as: 'Cities',
      where: {} // here goes your condition
    }]    
  }],
});

Upvotes: 1

Related Questions