[英]How can I sort the results of a Bookshelf.js query based on a column of the join table?
我正在尝试从Bookshelf.js查询中获取结果,并通过联接表中的列对它们进行排序。 但是,当添加“ withRelated”时,我只能获取目标表,而不能获取联接表本身。 下面是我到目前为止的代码。
MyTable.forge().query({where: {id: req.params.id}})
.fetch({
withRelated: [
{'children.children': (qb) => { qb.orderBy('position', 'asc'); }}
]
}).then(result => {
res.send(JSON.stringify({theResult: result}));
});
“ orderBy”应该按仅在联接表中的“位置”列进行排序。 看来“ qb”仅返回目标表的结果,而不返回联接表的结果。 我已经尝试过“ withPivot”,但没有得到正确的结果。
因此,在浏览了返回的对象并阅读了更多文档之后,我发现Bookshelf.js将orderBy的属性更改为“ _pivot_position”。 将“ pivot ”添加到列名称,这就是您要使用的名称。 但是,这似乎仅适用于以下情况。
withRelated: [
{'children': (qb) => { qb.orderBy('_pivot_position', 'asc'); }}
]
如上所示,我必须排除“ children.children”,而仅使用“ children”。 我现在想知道Bookshelf.js是否允许您从子元素中指定多个“ orderBy”列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.