我正试图从书架上得到结果。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\u位置” '添加到列名中,这就是您想要使用的。然而,这似乎只适用于以下情况。
withRelated: [ {'children': (qb) => { qb.orderBy('_pivot_position', 'asc'); }} ]