问题描述
我已将 sequelizejs 集成到我的 express 框架中.我配置了所有模型并尝试使用它构建我的第一个查询.
I've integrated sequelizejs into my express framework. I got all the models configured and was trying to build my first query with it.
我不断收到错误消息错误:模型未与 ModelTwo 关联!"
I keep getting the error "Error: Model is not associated to 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
//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.
Any ideas as to what may be going wrong? I'm using the latest version of sequelize 1.7.0-rc6.
推荐答案
这其实是另一种情况,抛出同样的错误,
This is actually another case which throws the same error,
当我在包含模型时尝试使用 as 选项进行别名时遇到了同样的问题.只要您为默认名称起别名,这不会引发错误,但如果您尝试不同的名称,则会出错.
I ran in to the same issue when I try to use as option for aliasing when including the model. This will not throw an error as long as you aliased the default name, but you'll get error if you try different name.
解决办法,
上面写的很清楚
如果关联是别名(使用 as 选项),您必须指定包含模型时的此别名.
If an association is aliased (using the
asoption), you must specify this alias when including the model.
这意味着,反之亦然
如果你想在包含模型时给模型起别名,它的关联必须是别名(使用 as 选项).
If you want to alias a model when including, it's association must be aliased (using the
asoption).
这篇关于sequelize Model.hasOne 错误:模型未关联到 ModelTwo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)