未处理的拒绝 SequelizeEagerLoadingError:modalB 与 modelA 无关 [英] Unhandled rejection SequelizeEagerLoadingError: modalB is not associated to modelA

查看:22
本文介绍了未处理的拒绝 SequelizeEagerLoadingError:modalB 与 modelA 无关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 sequelize.js ORM 的sails.js.这就是我定义我的模型以使用关联的方式:https://github.com/KSDaemon/sails-hook-sequelize

I'm using sails.js with sequelize.js ORM. This is how I define my models to work with the associations: https://github.com/KSDaemon/sails-hook-sequelize

模型用户:

module.exports = {
  attributes: {
    user_id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    username: Sequelize.STRING,
    password: Sequelize.STRING,
    createdAt: Sequelize.DATE,
  },
  associations: function () {
    Response.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });
    Response.hasMany(Response, { as: 'responses', foreignKey: 'user_id' });
  },
  defaultScope: function () {
    return {
      include: [
        { model: Post, as: 'posts' },
        { model: Response, as: 'responses' },
      ]
    };
  },
  options: {
    freezeTableName : false,
    tableName       : 'users',
    schema          : 'codigomx',
    classMethods    : {},
    instanceMethods : {},
    hooks           : {}
  }
};

和我的模型响应:

module.exports = {

  attributes: {
    response_id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    response_content: Sequelize.TEXT,
    createdAt: Sequelize.DATE
  },
  associations: function () {
    Response.belongsTo(User, { foreignKey: 'user_id', targetKey: 'user_id'});
    Response.belongsTo(Post, { foreignKey: 'post_id', targetKey: 'post_id'});
  },
  options: {
    freezeTableName : false,
    tableName       : 'responses',
    schema          : 'codigomx',
    classMethods    : {},
    instanceMethods : {},
    hooks           : {}
  }

};

但是当我尝试运行它时:

but when I try to run this:

User.findOrCreate({where: {username: 'user'}, defaults: {password: 'psw'}});

它给了我这个错误:未处理的拒绝 SequelizeEagerLoadingError:响应与用户无关!

It gives me this error: Unhandled rejection SequelizeEagerLoadingError: Response is not associated to User!

而且我确实有正确的关联,有人可以告诉我我的关联是否有问题吗?

and I do have the associations correctly, can someone tell me if my associations have something wrong?

这是我的仓库:https://github.com/AlfredoDaAs/codigomx

推荐答案

我认为用户模型中存在拼写错误.

I think there is a typo in the user model.

而不是 Response.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });,它应该是 User.hasMany(Post, { as: '帖子',外键:'user_id' });

这篇关于未处理的拒绝 SequelizeEagerLoadingError:modalB 与 modelA 无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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