0
我想在使用golang gorm的(my)sql表中創建一個自引用。目前我的代碼看起來是這樣的:gorm golang one2many同一張表
type Person struct {
gorm.Model
Name string
Children []*Person `gorm:"ForeignKey:ParentID"`
ParentID uint
}
func main() {
/* code to get database connection omitted */
p := &Person{Name:"Sally"}
db.Create(p)
children := []*Person{ {Name:"Jane", ParentID:p.ID},
{Name:"Tom", ParentID:p.ID}}
for _, child := range children {
db.Create(child)
}
var children2 []*Person
db.Model(p).Related(children2, "ParentID")
}
的代碼是一個錯誤「reflect.Value.Set使用不可尋址值」失敗。
有沒有人知道如何使用go gorm獲得這種關係?
提前感謝:)