从关系中选择特定字段

时间:2022-10-16 22:56:20

I'm building a webapp on node.js (sails) with waterline (using mysql adapter).

我正在使用水线(使用mysql适配器)在node.js(sails)上构建一个webapp。

I've managed to get relationships working like this:

我设法让关系像这样工作:

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

I can easily find the relationship records with the line:

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

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);

Everything works fine but I need to only select specific fields from post_statistics. This does not seem to do anything, though, I'd expect it to work:

一切正常,但我只需要从post_statistics中选择特定的字段。但是,这似乎没有做任何事情,我希望它可以工作:

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

Adding the field names to the initial select is even worse since the fields already target the post table.

将字段名称添加到初始选择更糟糕,因为字段已经定位到post表。

Any sane way to make this work or am I going to have to write the whole query?

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

1 个解决方案

#1


Currently .populate({ assoc, select: [] }) is not supported in waterline, more details in https://github.com/balderdashy/waterline/issues/919.

目前.populate({assoc,select:[]})在水线中不受支持,详情请参阅https://github.com/balderdashy/waterline/issues/919。

One possible alternative would be to run a SQL query via .query() (docs).

一种可能的替代方法是通过.query()(docs)运行SQL查询。

Another would be to submit a patch to waterline adding support for .populate({ assoc, select: [] }).

另一种方法是向水线提交补丁,添加对.populate的支持({assoc,select:[]})。

#1


Currently .populate({ assoc, select: [] }) is not supported in waterline, more details in https://github.com/balderdashy/waterline/issues/919.

目前.populate({assoc,select:[]})在水线中不受支持,详情请参阅https://github.com/balderdashy/waterline/issues/919。

One possible alternative would be to run a SQL query via .query() (docs).

一种可能的替代方法是通过.query()(docs)运行SQL查询。

Another would be to submit a patch to waterline adding support for .populate({ assoc, select: [] }).

另一种方法是向水线提交补丁,添加对.populate的支持({assoc,select:[]})。