我有以下人为的例子:
type Top struct {
ID uint `gorm:"primary_key"`
Name string
Middle []*Middle
}
type Middle struct {
ID uint `gorm:"primary_key"`
TopID int
Name string
Low []*Low
}
type Low struct {
ID uint `gorm:"primary_key"`
MiddleID int
Name string
Bottom []*Bottom
}
type Bottom struct {
ID uint `gorm:"primary_key"`
LowID int
Name string
}
top := Top{
Name: "Top",
Middle: []*Middle{
{
Name: "Middle",
Low: []*Low{
{
Name: "Low",
Bottom: []*Bottom{
{
Name: "Bottom",
},
},
},
},
},
},
}
if err := db.Save(&top).Error; err != nil {
log.Fatal("Got errors when saving calc", err.Error())
}
if err := db.Where("id = 1").
Preload("Middle.Low.Bottom").
First(&top).Error; err != nil {
log.Fatal(err)
}
这给了我以下输出:
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:81)
[2016-03-18 01:53:23] [4.37ms] INSERT INTO "tops" ("name") VALUES ('Top') RETURNING "tops"."id"
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:81)
[2016-03-18 01:53:23] [3.49ms] INSERT INTO "middles" ("name","top_id") VALUES ('Middle','1') RETURNING "middles"."id"
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:81)
[2016-03-18 01:53:23] [1.07ms] INSERT INTO "lows" ("middle_id","name") VALUES ('1','Low') RETURNING "lows"."id"
()
[2016-03-18 01:53:23] [9.16ms] INSERT INTO "bottoms" ("low_id","name") VALUES ('1','Bottom') RETURNING "bottoms"."id"
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:86)
[2016-03-18 01:53:23] [1.54ms] SELECT * FROM "tops" ORDER BY "tops"."id" ASC LIMIT 1
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:88)
[2016-03-18 01:53:23] [2.63ms] SELECT * FROM "tops" WHERE ("id" = '1') ORDER BY "tops"."id" ASC LIMIT 1
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:88)
[2016-03-18 01:53:23] [1.65ms] SELECT * FROM "middles" WHERE (top_id IN ('1')) ORDER BY "middles"."id" ASC
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:88)
[2016-03-18 01:53:23] [4.20ms] SELECT * FROM "lows" WHERE (middle_id IN ('1')) ORDER BY "lows"."id" ASC
(/host_home/workspace/golang/src/bitbucket.org/cloudgloballog/gormtest/main.go:88)
[2016-03-18 01:53:23] can't find field Bottom in []**main.Low
如果我只嵌套 Top -> Middle -> Low 并调用Preload("Middle.Low")
它可以正常工作。线索可能在双指针引用中[]**main.Low
,但我无法弄清楚如何正确执行此操作。
此级别可能不支持嵌套预加载。有人知道吗?