簡體   English   中英

Golang Go-PG關系遞歸查詢

[英]Golang go-pg relation recursive query

我將需要使用遞歸方法,例如:

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上實現遞歸聯接,如果有人有經驗請告訴我。 非常感謝。

您需要指出它實際上是第二個結構上的關系:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM