sequelize 1.6在changelog中有以下内容:
[FEATURE]为find和findAll添加了关联预取
问题是如何?
我定义了以下模型:
var self = {
Medium: client.define("Medium", {
name: Sequelize.STRING,
description: Sequelize.TEXT
},
User: client.define("User", {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
email: Sequelize.STRING,
aboutArt: Sequelize.TEXT,
bio: Sequelize.TEXT,
password: Sequelize.STRING,
description: Sequelize.TEXT
}
};
self.User.hasMany(self.Medium, { as: 'Media' });
self.Medium.hasMany(self.User);
for(var key in self){
var model = self[key];
model.sync();
}
稍后我取这样的用户:
User.find(id)
.success(function(record) {
//record has no media!
})
用户实例没有列表媒体。我如何自动获取关联?
答案 0 :(得分:28)
顺便说一句:现在Sequelize 1.6.0已经出来了,热切加载的语法略有改变:
User.find({ where: {id: id}, include: [Media] }).success(function(user){
console.log(user.media)
})
因此,您必须将Model而不是Model的名称传递给include语句。
答案 1 :(得分:6)
sdepold实际上编写了代码,但我相信他所确定的语法包括:
User.find({ where: {id: id}, include: ['Media'] }).success(function(user){
console.log(user.media)
})
答案 2 :(得分:4)
答案目前仅在代码,测试和......我的头脑中可用:D
User.find({ where: {id: id}, include: ['Media'] }).success(function(user){
console.log(user.media)
})
尚未提供文档的原因是,该版本目前是alpha版本。