I have two models Doctor and Degree with many to many relationship.
Doctor model:
const Doctor = _sequelize.define('doctor', {
fullname: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
},
gender: {
type: Sequelize.STRING
},
address: {
type: Sequelize.STRING
},
phone: {
type: Sequelize.STRING
},
avatarurl: {
type: Sequelize.STRING
},
active: {
type: Sequelize.BOOLEAN,
defaultValue: true
},
license: {
type: Sequelize.STRING
}
},{
timestamps: false,
tableName: 'doctor'
})
Doctor.associate = (models) => {
Doctor.belongsToMany(model.degree, { through: 'doctor_degree', foreignKey: 'doctor_id',as: 'degree' })
}
Degree model:
const Degree = _sequelize.define('degree', {
name: {
type: Sequelize.STRING
}
},{
timestamps: false,
tableName: 'degree'
})
Degree.associate = (models) => {
Degree.belongsToMany(models.doctor, { through: 'doctor_degree', foreignKey: 'degree_id' })
};
Doctor_Degree model:
const Doctor_Degree = _sequelize.define('doctor_degree', {
doctor_id: {
type: Sequelize.STRING,
allowNull: false,
references: {
model: 'doctor',
key: 'id'
}
},
degree_id: {
type: Sequelize.STRING,
allowNull: false,
references: {
model: 'degree',
key: 'id'
}
}
},{
timestamps: false,
tableName: 'doctor_degree'
});
And I have a service to find all doctor with degree
var result;
try{
await doctorModel.findAll({
attributes: ['id','fullname', 'email','gender','address','phone','avatarurl','active','license'],
include: [{
model: degreeModel,
as: 'degree'
}]
}).then((doctors) => {
result = doctors
})
}
catch(err){
throw err
}
return result
But i got the following error:
[SequelizeEagerLoadingError]: degree is not associated to doctor!
Can anyone suggest what I am doing wrong?