2015-05-09 136 views
2

我正在用waterline(使用mysql适配器)在node.js(sails)上构建webapp。从关系中选择特定字段

我已经成功地获得工作这样的关系:

//Post.js attributes 
    post_statistics: { 
     collection: 'postStatistics', 
     via: 'post' 
    } 

我可以很容易地找到与行的关系记录:

var query = {user:userId}; // Just filtering by the user 
query.is_public = Post.POSTS_ONLY_ACTIVE; // Only showing active posts, boolean 
query.select = ['id','title','content']; // id is only needed for me to get the join working 
Post.find(query).populate('post_statistics').exec(cb); 

一切工作正常,但我只需要选择特定的来自post_statistics的字段。 这似乎并没有做任何事情,不过,我希望它的工作:

Post.find(query).populate('post_statistics',{select:['post','user','id']}).exec(cb); 

添加字段名称初始选择更是雪上加霜,因为栏位已针对表。

任何理智的方法,使这项工作,或者我将不得不编写整个查询?

回答

2

目前.populate({ assoc, select: [] })在水线不支持,更多详细信息在https://github.com/balderdashy/waterline/issues/919

一种可能的选择是通过.query()docs)运行SQL查询。

另一个将提交补丁waterline添加支持.populate({ assoc, select: [] })

+0

感谢您的回复。我实际上已经做了一些研究,并且水线似乎对select有支持,它可以与磁盘适配器一起创造奇迹。但我已经尝试将它切换到mysql-sails并且它在那里不起作用。所以问题必须存在于mysql-sails中,我想。不管怎么说,我会在线程上做更多关于github的评论。 – Andrius

+0

不客气。那么,它不在[水线文档](https://github.com/balderdashy/waterline-docs),也不在[水线适配器测试](https://github.com/balderdashy/waterline-adapter-tests)所以我不会认为它是“官方支持”的。换句话说,您可能会在给定的适配器中运行,但由于没有明确支持,后续更改可能会无意中破坏该行为。 –

相关问题