Sequelize Many to Many failing with 'is not associated to' when trying to associate entries?

我只是一个虾纸丫 提交于 2019-12-06 07:44:48

In through relationships the following should work

ArticleKeyword.findAll({
  include: [{
    model: SiteArticle,
    through: {
      attributes: ['createdAt', 'startedAt', 'finishedAt'],
      where: {siteArticleId: siteArticleId
    }
  }]
});
Andre M

The issue turns out that the reason site_article_keyword is not associated is because it is the association! With that in mind the code becomes:

return ArticleKeyword.findAll({
    where: {
        language: language,
    },
    include: [{
            model: SiteArticle,
            as: 'SiteArticle',
            siteArticleId: siteArticleId
        }]
    });

BTW one minor tweak to my code, is in the inclusion of 'as' to the belongsToMany:

ArticleKeyword.belongsToMany(
    SiteArticle,
    { through: SiteArticleKeyword, as: 'SiteArticle' }
);

SiteArticle.belongsToMany(
    ArticleKeyword,
    { through: SiteArticleKeyword, as: 'ArticleKeyword' }
);

This allows for addArticleKeyword() instead of addArticle_Keyword().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!