尝试关联条目时,Sequelize Many to Many 失败并显示“未关联到"? [英] Sequelize Many to Many failing with 'is not associated to' when trying to associate entries?

查看:29
本文介绍了尝试关联条目时,Sequelize Many to Many 失败并显示“未关联到"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Sequelize 进行多对多配置时遇到问题,它抱怨 site_article_keyword 与 article_keyword 没有关联.下面的代码代表了一个最小的测试用例,试图了解我做错了什么(我希望提供一些更小的东西,但这就是我所拥有的).我将 bluebird 用于 Promise API.

I am having a problem with my many to many configuration with Sequelize, where it complains that site_article_keyword is not associated to article_keyword. The code below represents a minimal test case to try to understand what I am doing wrong (I hoped to provide something smaller, but this is what I have). I am using bluebird for the Promise API.

const Sequelize = require('sequelize');

var sequelize = new Sequelize(undefined, undefined, undefined, {
    dialect: 'sqlite',
    storage: './mydatabase',
});

const SiteArticle = sequelize.define('site_article', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    ownerId: {
        type: Sequelize.INTEGER,
        field: 'owner_id'
    },
    title: Sequelize.STRING
        // other fields omitted
}, {
    timestamps: true
});

const ArticleKeyword = sequelize.define('article_keyword', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: Sequelize.STRING,
    language: Sequelize.STRING
        // other fields omitted
}, {
    timestamps: true
});

const SiteArticleKeyword = sequelize.define('site_article_keyword', {
    siteArticleId: {
        type: Sequelize.INTEGER,
        field: 'site_article_id',
        references: {
            model: SiteArticle,
            key: 'id'
        }
    },
    articleKeywordId: {
        type: Sequelize.INTEGER,
        field: 'article_keyword_id',
        references: {
            model: ArticleKeyword,
            key: 'id'
        }
    }
    // other fields omitted
}, {
    timestamps: true
});

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

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

这就是定义的模型,现在用于尝试创建源条目和目标条目,然后我想将它们关联起来.失败发生在我调用 ArticleKeyword.findAll() 的行上:

That's the model defined, now for trying to create the source and destination entries, that I then want to associate. The failure happens on the line where I call ArticleKeyword.findAll():

sequelize.sync({}).then(function() {

    // populate some data here

    let siteArticle;

    SiteArticle.create({
        ownerId: 1,
        title: 'hello world'
    }).then(function(entry) {
        siteArticle = entry;
        console.log('site article: ', JSON.stringify(entry, undefined, 2));

        return ArticleKeyword.findOrCreate({
            where: {
                name: 'keyword1',
                language: 'en'
            }
        });
    }).spread(function(entry, success) {
        console.log('article keyword: ', JSON.stringify(entry, undefined, 2));
        return siteArticle.addArticle_keyword(entry);
    }).spread(function(entry, success) {
        console.log('site article keyword: ', JSON.stringify(entry, undefined, 2));

        const siteArticleId = 1;
        const language = 'en';

        return ArticleKeyword.findAll({
            where: {
                language: language,
            },
            include: [{
                model: SiteArticleKeyword,
                where: {
                    siteArticleId: siteArticleId
                }
            }]
        });
    }).then(function(articleKeywords) {
        if (articleKeywords) {
            console.log('entries: ', JSON.stringify(articleKeywords, undefined, 2));
        } else {
            console.log('entries: ', 'none');
        }
    }).catch(function(error) {
        console.log('ERROR: ', error);
    }.bind(this));

}).catch(function(error) {
    console.log(error);
});

我的代码基于 'Mixin BelongsToMany' Sequelize 文档中的示例.

I am basing my code on the 'Mixin BelongsToMany' example in the Sequelize documentation.

谁能建议我做错了什么?

Can anyone suggest what I am doing wrong?

推荐答案

通过关系,以下应该起作用

In through relationships the following should work

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

这篇关于尝试关联条目时,Sequelize Many to Many 失败并显示“未关联到"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆