【问题标题】:Golang go-pg relation recursive queryGolang go-pg 关系递归查询
【发布时间】:2019-01-08 00:50:51
【问题描述】:

我需要加入递归,像这样:

SELECT a.*, b.* c.* FROM a
LEFT JOIN b on b.id = a.b_id
LEFT JOIN c ON c.id = b.c_id

我的模型定义是:

type A struct {
    ID int,
    NameA string,
    B_id int
    B *B,
    C *C,
}

type B struct {
    ID int,
    C_id int,
    NameB string,
    C C,
}

type C struct {
    ID int,
    NameC string,
}

我尝试使用关系但没有用:

a := A{}
//does not work
db.Model(&a).Relation("B").Relation("C").First()

//works
db.Model(&a).Relation("B").First()

如何在 go-pg 上实现递归连接,如果有人对此有经验,请告诉我。非常感谢。

【问题讨论】:

    标签: go go-pg


    【解决方案1】:

    您需要指出它实际上是第二个结构上的关系:

    db.Model(&a).Relation("B").Relation("B.C").First()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2013-04-07
      • 2023-03-04
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多