请教下Grom关联查询

infoBrainSys · · 1323 次点击
真棒,豁然开朗~ 谢谢指点
#2
更多评论
```go type Person struct { gorm.Model Name string // 姓名 Hobbies []Hobby `gorm:"many2many"` } type Hobby struct { gorm.Model Desc string // 兴趣描述 Persons []Person `gorm:"many2many"` } ``` ```go func TestMigrate(t *testing.T) { db.Client().AutoMigrate(&Person{}, &Hobby{}) } func TestAddPerson(t *testing.T) { p := Person{ Name: "张三", Hobbies: []Hobby{ {Desc: "玩游戏"}, {Desc: "看电影"}, {Desc: "旅游"}, }, } db.Client().Create(&p) } func TestFindHobby(t *testing.T) { var hobby Hobby _ = db.Client().Preload("Persons").Find(&hobby) t.Logf("%+v", hobby) } ```
#1