我试图建立一个简单的n:m关系,案例是,一个医生可以有很多患者,患者可以有很多医生...
Doctor Model
findViewById()
患者模型
module.exports = (sequelize, DataTypes) => {
const Doctor = sequelize.define('Doctor', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
doc_crm: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'doctors',
timestamps: false
})
Doctor.associate = function (models) {
Doctor.belongsToMany(models.Patient, { as: 'Patients', through: { model: models.PatientDoctor } })
Doctor.belongsTo(models.User, { foreignKey: 'user_id' })
}
return Doctor
}
加入表是
PatientDoctor
module.exports = (sequelize, DataTypes) => {
const Patient = sequelize.define('Patient', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
tableName: 'patients',
timestamps: false
})
Patient.associate = function (models) {
Patient.belongsToMany(models.Doctor, { as: 'Doctors', through: { model: models.PatientDoctor } })
Patient.belongsTo(models.User, { foreignKey: 'user_id' })
}
return Patient
}
所以,当我查询通过Patient和Doctor的PatientDoctor模型时,我得到了一个
module.exports = (sequelize, DataTypes) => {
const PatientDoctor = sequelize.define('PatientDoctor', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, {
tableName: 'patient_doctors',
timestamps: false
})
return PatientDoctor
}
我做错了什么?我尝试了很多东西,都没有运气。
提前致谢!