項目中多對多關聯應用(請您多多留言指教):
例子:
第一步: 創建表及關聯表
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爲中間表的外鍵屬性關聯