SequelizeJS属于许多协会

时间:2019-08-05 05:09:04

标签: javascript node.js sequelize.js

我正在使用 Sequelize v5和 pg v7.9。 这些是我的协会。

这是我的文章模型

Article.associate = models => {
Article.belongsToMany(models.Article, {
  through: 'ArticleArticleTag',
  as: 'article_tags',
  foreignKey: 'article_id'
});};

这是我的商品标签模型

ArticleTag.associate = models => {
ArticleTag.belongsToMany(models.Article, {
  through: 'ArticleArticleTag',
  as: 'articles',
  foreignKey: 'article_tag_id'
});};

这是我的ArticleArticleTag模型

ArticleArticleTag.associate = function(models) {
ArticleArticleTag.belongsTo(models.Article, { foreignKey: 'article_id' });
ArticleArticleTag.belongsTo(models.ArticleTag, { foreignKey: 'article_tag_id' });};

我尝试对belongsToMany关联使用不同的方法。现在,这是我用文章标签创建文章的代码:

await articleTags.forEach(async articleTagId => {
  await models.ArticleTag.findOne({ where: {id: articleTagId }})
    .then(async tag => {
      await createdArticleService.setArticle_tags([tag.id]).catch(err => console.log(err));
    });
});

这是我从Sequelize Database得到的示例错误响应。

{ SequelizeDatabaseError: syntax error at or near "f5ad1d3b5fd"
at Query.formatError (/node_modules/sequelize/lib/dialects/postgres/query.js:363:16)
at query.catch.err (/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
at tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:517:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:574:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:619:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:695:18)
at _drainQueueStep (/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
at process.topLevelDomainCallback (domain.js:120:23)name: 'SequelizeDatabaseError',parent:...

sql:'INSERT INTO "article_article_tag" ("id","article_id","article_tag_id") VALUES (c425ebb0-1085-4885-9370-3f5ad1d3b5fd,\'5487bda5-7026-46e0-9ba1-b2b7a53c8773\',\'f1384041-dda4-405a-ab73-ebf3396b39e7\');'

我的问题是,我已经尝试按照Sequelize的文档进行归属关系,但是我仍然无法使其正常工作。我不知道错误是否是因为我的代码。我仍然不知道如何解决此问题。我希望有人能帮助我。

谢谢

0 个答案:

没有答案