我正在使用 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
我是一名优秀的程序员,十分优秀!