Sequelize很多很多人因为'而失败了。在尝试关联条目时?

时间:2017-02-06 21:57:44

标签: node.js sequelize.js

我遇到了Sequelize的多对多配置问题,抱怨site_article_keyword is not associated to article_keyword。下面的代码代表了一个最小的测试用例,试图理解我做错了什么(我希望提供更小的东西,但这就是我所拥有的)。我使用bluebird作为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()的行:

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文档中的示例。

任何人都可以建议我做错了吗?

2 个答案:

答案 0 :(得分:1)

通过关系,以下内容应该有效

ActiveSignalForm.choices

答案 1 :(得分:1)

问题是site_article_keyword没有关联的原因是因为它是关联!考虑到这一点,代码变为:

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

BTW对我的代码进行了一次小调整,其中包括' as'到belongsToMany:

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

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

这允许addArticleKeyword()代替addArticle_Keyword()