gpt4 book ai didi

javascript - 未处理的拒绝 SequelizeEagerLoadingError : modalB is not associated to modelA

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:02 34 4
gpt4 key购买 nike

我正在使用 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:响应未与用户关联!

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

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

最佳答案

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

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

关于javascript - 未处理的拒绝 SequelizeEagerLoadingError : modalB is not associated to modelA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50574687/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com