SequelizeEagerLoadingError,无法通过包含获取数据

时间:2018-10-15 20:22:24

标签: javascript node.js express sequelize.js

我使用Sequelize.js向数据库发出请求。我在两个表之间存在多对多关系,并且制作了第三个联结表。这是我的三张桌子。 polit_in_article是联结表。

politician.js:

module.exports = (sequelize, DataTypes) => {
    const Politician = sequelize.define('politician', {
        ...
    });

    Politician.associate = (models) => {
        Politician.hasMany(models.Polit_in_article, {
            foreignKey: 'politicianId',
            as: 'polit_in_articles',
        });
    };

    return Politician;
};

article.js:

module.exports = (sequelize, DataTypes) => {
    const Article = sequelize.define('article', {
        ...
    });

    Article.associate = (models) => {
        Article.hasMany(models.Politician, {
            foreignKey: 'articleId',
            as: 'polit_in_articles'
        });
    };
    return Article;
};

polit_in_article.js:

module.exports = (sequelize, DataTypes) => {
    const Polit_in_article = sequelize.define('polit_in_article', {
        times_mentioned: DataTypes.INTEGER,
    });
    Polit_in_article.associate = (models) => {
        Polit_in_article.belongsTo(models.Article, {
            // foreignKey: 'articleId',
            as: 'articles',
        });
        Polit_in_article.belongsTo(models.Politician, {
            // foreignKey: 'politicianId',
            as: 'politicians',
        });
    };

    return Polit_in_article;
};

这是我的控制器(不确定这是调用它的正确方法):

const Sequelize = require('sequelize');

const Article = require('../models').Article;
const Politician = require('../models').Politician;
const Polit_in_article = require('../models').Polit_in_article;

const Op = Sequelize.Op;

module.exports = {
    getAllArticlesOfPolitician(req) {
        return Article
            .findAll({
                include: [{
                    model: Polit_in_article,
                    include: [{
                        model: Politician,
                        where: {
                            lastname: req.query.politicianLastName,
                        }
                    }],
                }],
            }).then(response => response)
            // .catch(err => err);
    }
};

,由此我得到一个错误SequelizeEagerLoadingError: polit_in_article is not associated to article。我可能错过了一些东西,但是我真的不知道该怎么办。我试图使Articles表具有关联as: 'polit_in_articles',直到我需要将政治人物表也加入到查询中为止。

2 个答案:

答案 0 :(得分:0)

您需要在关联中使用through关键字。 Articles将具有politicians的{​​{1}} through模型,但是由于您仅使用它的主键,因此可以推断出它,而省去Polit_in_article并使用字符串命名表。

polit_in_article.js

现在,您可以通过指定Politician.associate = (models) => { Politician.hasMany(models.Article, { foreignKey: 'politicianId', through: 'polit_in_article', as: 'articles', }); }; Article.associate = (models) => { Article.hasMany(models.Politician, { foreignKey: 'articleId', through: 'polit_in_article', as: 'politicians', }); }; as来查询联接表。

through

答案 1 :(得分:0)

我只需要在article.js中修复关联,我与政客而不是联结表进行关联:

module.exports = (sequelize, DataTypes) => {

    const Article = sequelize.define('article', {
         ...
    });

    Article.associate = (models) => {
        Article.hasMany(models.Polit_in_article, {
            foreignKey: 'articleId',
            as: 'polit_in_articles'
        });
    };
    return Article;
};