更新时间:2023-01-14 21:36:26
这就是您要寻找的:
db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
defer db.Close()
story := &Story{}
db.Preload("Paragraphs").Preload("Paragraphs.Sentences").First(story, 1)
它找到 id = 1
的故事并预载其关系
It finds the story with the id = 1
and preloads its relationships
fmt.Printf("%+v\n", story)
这为您很好地打印了结果
This prints out the result nicely for you
旁注:您可以打开Gorm的日志模式,以便查看基础查询,进行调试或任何其他用途:
Side note: You can turn on log mode of Gorm so that you can see the underlying queries, to debug, or any other purposes:
db.LogMode(true)