我已将 sequelizejs 集成到我的 express 框架中。我配置了所有模型并尝试使用它构建我的第一个查询。
我不断收到错误消息“错误:模型未与 ModelTwo 关联!”
app.get('/',function(req,res){
db.Member.findAll({include:[{model:db.MemberProfile,as:'Profile'}]})
.success(function(users){
if(users){
res.json(users);
}else{
res.send('no users');
}
});
});
//模型.js
module.exports = function(sequelize,sql) {
return sequelize.define('Model', {
//attributes
});
Model.hasOne('ModelTwo',{foreignKey:'model_id'});
};
//model_two.js
module.exports = function(sequelize,sql) {
return sequelize.define('ModelTwo', {
//attributes
});
//no relationship defined. Tried ModelTwo.hasMany('Model',{foreignKey:'id'});
//but it yields the same resulting error
};
关于可能出现什么问题的任何想法?我用的是最新版的sequelize 1.7.0-rc6。
最佳答案
这实际上是另一种情况,它会抛出相同的错误,
当我在包含模型时尝试使用 as
选项进行别名时遇到了同样的问题。只要您为默认名称起别名,这不会引发错误,但如果您尝试不同的名称,则会出错。
解决办法,
上面写的很清楚
If an association is aliased (using the
as
option), you must specify this alias when including the model.
这意味着,反之亦然
If you want to alias a model when including, it's association must be aliased (using the
as
option).
关于javascript - Sequelize Model.hasOne 错误 : Model is not associated to ModelTwo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21835827/