sequelize BelongsToMany多对多关系(Many-to-many association)应用

项目中多对多关联应用(请您多多留言指教):

例子:

第一步: 创建表及关联表

1、学生表student : id、name、sex等

2、城市表city : id、province、city、county等

3、学生与城市关联表stu_city_info:id、stu_id、city_id等

 

第二步:sequelizejs 创建关联

Department.associate = function() {

const {Student, City, StuCityInfo } = app.model

Student.belongsToMany(City, { through: StuCityInfo , foreignKey: 'stuId', otherKey: 'cityId' })

}

 

详解: belongsToMany 多对多关系 ,源表Student、、源表City、关联表StuCityInfo(即中间表)

foreignkey、otherkey为中间表的外键属性关联

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章