Sails.js中两个模型的关联
Associations of two Models in Sails.js
我遇到了帆船中两个模型的链接问题。我有两个模特:歌手和乡村。在'Singer'模型中,我有一个属性'singer_country',它表示'Country'模型的id。我的问题是,当我显示所有歌手的属性时,我可以在'Country'模型中获得属性'country_name'。我不知道怎么做。下面是我的代码:我的"歌手"模型
module.exports = {
attributes: {
singer_name:{
type: 'string',
required: true,
size: 50
},
singer_realname:{
type: 'string',
size: 50
},
singer_gender:{
type: 'string',
enum: ['Male', 'Female'],
defaultsTo: 'Male'
},
singer_brithday:{
type: 'int'
},
singer_image:{
type: 'string'
},
singer_description:{
type: 'string'
},
singer_country:{
type: 'string'
}
}
};
我的"国家"模型:
module.exports = {
attributes: {
country_name: {
type: 'string'
}
}
};
显示歌手属性的方法:
index: function(req, res, next){
Singer.find().exec(function foundSinger(err, singerObj){
if(err) return next(err);
res.view({
singers: singerObj,
});
});
},
我的数据库是MongoDB,我使用的是风帆beta 0.10 rc8。谢谢你的帮助。
singer_country:{
model: 'Country'
}
Singer.find().populate('singer_country').exec(function foundSinger(err, singerObj){
if(err) return next(err);
res.view({
singers: singerObj,
});
});
这里还有很多其他的变量,但是如果你的关联是一对一的那么你的singer_country属性就需要像
这样singer_country: {model: 'country'}
https://github.com/balderdashy/waterline-docs/blob/master/associations.md 相关文章:
- sailsjs在创建两个模型时的错误处理
- 保存两个模型(属于第三个模型)和一个提交
- 使用包含过滤器在环回中连接两个模型
- 比较主干中的两个模型属性
- 在 ng 模型中具有单向绑定的两个输入
- 在一个输入文本框中绑定两个模型名称
- AngularJS - $watch内部的初始化,取决于两个模型
- 我需要在两个模型(rails)之间创建一个新关系,然后通过javascript更改关系模型的属性
- Rails-从两个模型中获取json-格式化json
- 如何使用AngularJs在html中定义两个模型
- 将两个模型与一个模型传递到主干比较器,并进行字母数字排序
- 在主干的一个视图中使用两个模型时出现问题
- 我是否需要在环回中声明两个模型的关系类型?
- Sails.js中两个模型的关联
- 如何在两个模型上重复
- 模板两个模型在一个视图-骨干/木偶
- 关闭和打开两个模型弹出窗口随后在按钮's使用JQuery的Click事件
- 在 SAPUI5 中绑定两个模型
- AngularJS 使用 onSubmit 使用两个模型更新输入
- 是否可以在一个 asp.net mvc 视图中有两个模型