Unhandled rejection SequelizeEagerLoadingError: modalB is not associated to modelA
Unhandled rejection SequelizeEagerLoadingError: modalB is not associated to modelA
我正在使用 sails.js 和 sequelize.js ORM。这就是我定义模型以与关联一起工作的方式:
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 : {}
}
};
但是当我尝试 运行 时:
User.findOrCreate({where: {username: 'user'}, defaults: {password: 'psw'}});
它给我这个错误:未处理的拒绝 SequelizeEagerLoadingError:响应与用户无关!
而且我确实有正确的联想,如果我的联想有问题,有人能告诉我吗?
我认为用户模型中有错字。
而不是Response.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });
,应该是User.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });
我正在使用 sails.js 和 sequelize.js ORM。这就是我定义模型以与关联一起工作的方式: 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 : {}
}
};
但是当我尝试 运行 时:
User.findOrCreate({where: {username: 'user'}, defaults: {password: 'psw'}});
它给我这个错误:未处理的拒绝 SequelizeEagerLoadingError:响应与用户无关!
而且我确实有正确的联想,如果我的联想有问题,有人能告诉我吗?
我认为用户模型中有错字。
而不是Response.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });
,应该是User.hasMany(Post, { as: 'posts', foreignKey: 'user_id' });