【问题标题】:Sequelize request error SequelizeEagerLoadingErrorSequelize 请求错误 SequelizeEagerLoadingError
【发布时间】:2018-09-17 18:48:02
【问题描述】:

我是 sequelize 的新手,我正在尝试使用关联表提出请求

我有一个名为Experience的第一个模型

module.exports = function (sequelize, DataTypes) {
    const Experience = sequelize.define('experience', {
        internalId: {
            type: DataTypes.BIGINT,
            unique: true,
            allowNull: false,
        },
        label: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: false,
        },
        picture: {
            type: DataTypes.TEXT,
            unique: false,
            allowNull: true,
        },
        type: {
            type: DataTypes.STRING,
            validate: {
                isIn: {
                    args: [[
                        'generic',
                        'specific',
                    ]],
                    msg: 'Must be a valid type',
                },
            },
            unique: false,
            allowNull: true,
        },
        author: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: true,
            defaultValue: 'import',
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true,
        },
    });

    Experience.associate = (models) => {
        Experience.belongsToMany(models.Tag, {
            as: 'Tags',
            through: models.ExperienceTag,
        });
    };

    return Experience;
};

一秒钟叫Tags

module.exports = function (sequelize, DataTypes) {
    const Tag = sequelize.define('tag', {
        internalId: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false,
        },
        name: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: false,
        },
        author: {
            type: DataTypes.STRING,
            unique: false,
            allowNull: true,
            defaultValue: 'import',
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true,
        },
    });

    Tag.associate = (models) => {
        Tag.belongsToMany(models.Experience, {
            as: 'Experiences',
            through: models.ExperienceTag,
        });
    };

    return Tag;
};

关联表名称为ExperienceTags 我想得到所有有tagId = 44Experience

这是我的要求:

Experience.findAll({
        include: [{
            model: ExperienceTag,
            where: { tagId: 44 },
        }],
    })
        .then((results) => {
            winston.warn(JSON.stringify(results, null, 2));
            res.status(200)
                .send(results);
        })
        .catch(error => res.status(500)
            .send({ error: error.toString() }));

但是当我执行它时出现如下错误:

{
  "error": "SequelizeEagerLoadingError: experienceTag is not associated to experience!"
}

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    我认为您喜欢包含Tag 而不是ExperienceTag,以下示例可能会对您有所帮助

    Experience.findAll({
            include: [{
                model: Tag, //model name which you want to include
                as: 'Tags', // you have to pass alias as you used while defining
                where: { tagId: 44 },
            }],
        })
    

    【讨论】:

      【解决方案2】:

      我认为,您需要在包含中添加 as: 'Experiences' ,因为您已经定义了与别名的关联

      改变这个

      Experience.findAll({
          include: [{
              model: ExperienceTag,
              where: { tagId: 44 },
          }],
      })
      

      Experience.findAll({
          include: [{
              model: ExperienceTag,
              as: 'Experiences', // <---- HERE
              where: { tagId: 44 },
          }],
      })
      

      【讨论】:

      • 不工作,但如果我尝试使用 ```` Experience.findAll({ include: [{ model: Tag, as: 'Tags', where: { id: 44 }, }], } ) ```看起来不错
      猜你喜欢
      • 2020-08-14
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多