Bonjour j'ai un problème d'association entre mes models . Pourtant du coté de ma bdd les relations sont bien là. Voici mon code :
j'ai un model user avec une association:
" User.associate = models => {
User.hasMany(models.Annonce,
{as:'Annonce',foreignKey:"ID_user"}
);
};"
ensuite du côté de mon model annonce:
"Annonce.associate = (models) => {
Annonce.belongsTo(models.user, {
// foreignKey: "ID_user",
// sourceKey: "ID_user",
// onDelete: "cascade",
as: 'ID_user',
foreignKey:{
name:"ID_user",
allowNull:false
}
});
};"
Ce que je veux
Je voudrais que ce code fonctionne :
"exports.getAllAnnounces = async (req, res) => {
try {
const announces = await Annonce.findAll({include:["User"]});
res.send(announces);
} catch (err) {
console.log(err);
}
};"
Ce que j'obtiens
j'obtiens l'erreur suivante :
EagerLoadingError [SequelizeEagerLoadingError]: Users is not associated to Annonces!
at Function._getIncludedAssociation (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:565:13)
at Function._validateIncludedElement (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:502:53)
at C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:421:37
at Array.map (<anonymous>)
at Function._validateIncludedElements (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:417:39)
at Function.findAll (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:1103:12)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at Function._defaultsOptions (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:653:17)
at Function._injectScope (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:1986:10)
at Function.findAll (C:\Users\LyFra\Desktop\api_skill_game\node_modules\sequelize\lib\model.js:1090:10)
at exports.getAllAnnounces (C:\Users\LyFra\Desktop\api_skill_game\controllers\AnnounceControllers.js:6:39)