代码之家  ›  专栏  ›  技术社区  ›  Nicholas Gati

我如何对书架上的结果进行排序。基于连接表列的js查询?

  •  0
  • Nicholas Gati  · 技术社区  · 7 年前

    我正试图从书架上得到结果。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”,但没有得到正确的结果。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Nicholas Gati    7 年前

    因此,在查看返回的对象并阅读更多文档后,我发现orderBy的属性因Bookshelf而异。js到“pivot\u位置” '添加到列名中,这就是您想要使用的。然而,这似乎只适用于以下情况。

    withRelated: [
          {'children': (qb) => { qb.orderBy('_pivot_position', 'asc'); }}
        ]