我试图从 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”应该按仅在连接表中的“position”列进行排序。看来“qb”只返回目标表的结果,而不返回连接表的结果。我尝试过“withPivot”,但没有得到正确的结果。
最佳答案
因此,在查看返回的对象并阅读更多文档后,我发现 orderBy 的属性被 Bookshelf.js 更改为“_pivot_position”。 “pivot”被添加到列名称中,这就是您想要使用的。然而,这似乎只适用于以下情况。
withRelated: [
{'children': (qb) => { qb.orderBy('_pivot_position', 'asc'); }}
]
如上所示,我必须排除“children.children”并仅使用“children”。我现在想知道 Bookshelf.js 是否允许您从子元素指定多个“orderBy”列。
关于javascript - 如何根据连接表的列对 Bookshelf.js 查询的结果进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45000365/