I have 2 models Assignment and Area in defined each one in seperate file like this :
./models
--Area.js
--Assignment.js
--index.js
Area.js :
const Area = sequelize.define('area',{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: Sequelize.STRING,
}
}, {
timestamps: false
});
Area.hasMany(Assignment, { foreignKey:'areaId'});
Assignment.js :
const Assignment = sequelize.define('assignment',{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
}
}, {
timestamps: false
});
Assignment.associate = models => {
models.Assignment.belongsTo(models.Area, {
foreignKey: 'areaId',
onDelete: "CASCADE",
});
};
When i try to find some assignments with areas information (join) with Include property i got an error saying that : area is not associated to assignment!
Assignment.findAll({
include: [{ model: Area }]
}).then((assignments) => {
res.status(200).json(assignments)
}).catch((e) => {
res.status(500).json({ error: e.message })
})
Thanks in advance.