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爲中間表的外鍵屬性關聯

 

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