Business background
there are three table relationships as follows:
- Master Table
teacher
:id
,name
- Student Table
student
:id
,name
- teacher-student relationship table
relation
:id
,teacher_id
,student_id
the above three tables have established foreign key relationships in mysql
question
how to query all the student information corresponding to a teacher in sequelize
?
according to official documents, only two tables can be jointly queried, but it cannot be figured out if it involves three tables.
Code
db.teacher.hasMany(db.relation,{
foreignKey:`teacher_id`
});
db.relation.hasMany(db.student,{
foreignKey:`id`,
targetKey:"student_id`
})
//???????????~~~