svoruganti
svoruganti

Reputation: 672

sql/sequelize how to make a complex query involving one-to-many relation

I have two models, a Session and a Register, with a relation defined as follows:

Session.hasMany(Register, {
  foreignKey: {
    allowNull: false,
  },
});
Register.belongsTo(Session);

I want to get a list of Sessions that the User has registered for, a Register has a foreign key with the User model. With only the UserId how would I get a list of Sessions that have a Register object created by the User?

Upvotes: 0

Views: 37

Answers (1)

eliday
eliday

Reputation: 186

You can nest your include so that the user model includes the register which incldes the session like the example below: models.users.findOne({ where: { email: data.email //or any other unique identifier }, include: { model: models.register, include: {model: models.sessions}} })

Upvotes: 1

Related Questions