我是使用 Sequelize 的新手,我想知道是否可以设置 one-to-many through
关系。
在下面的案例中可以看到这种关系的一个可能案例;A school has many students through a Class
A student belongs to a school through Class
最佳答案
是的。这条路:
School.belongsToMany(Student, { through: 'school_student' });
Student.belongsTo(School, { through: 'school_student'});
请注意,这意味着存在一个“school_student”表,如果您使用 Model.sync(),它将自动为您创建,但如果没有,您需要自己创建它(最好通过迁移)。如果创建它,请确保它具有 schoolId 和 studentId 列。
关于database - 在 Sequelize 中建立一对多的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60978071/