Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

sequelize.js - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}} })


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...