sequelize 在 find (1.6) 上获取关联 [英] sequelize fetching associations on find (1.6)

查看:10
本文介绍了sequelize 在 find (1.6) 上获取关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sequelize 1.6 在更新日志中有以下内容:

sequelize 1.6 has the following in the changelog:

[FEATURE] 为 find 和 findAll 添加关联预取

[FEATURE] added association prefetching for find and 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();
}

稍后当我获取这样的用户时:

later when i fetch a user like this:

 User.find(id)
    .success(function(record) {
        //record has no media!
    })

用户实例没有列表媒体.如何自动获取关联?

the User instance does not have a list media. How do i auto fetch associations?

推荐答案

顺便说一句:现在 Sequelize 1.6.0 已经发布,急切加载的语法略有变化:

BTW: Now that Sequelize 1.6.0 is out, the syntax for the eager loading has slightly changed:

User.find({ where: {id: id}, include: [Media] }).success(function(user){ 
  console.log(user.media)
})

因此,您必须将模型而不是模型的名称传递给包含语句.

So you'll have to pass the Model instead of the Model's name to the include statement.

这篇关于sequelize 在 find (1.6) 上获取关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆