![](/img/trans.png)
[英]Golang GORM data retrieve for many to many relationship with join table
[英]One to many relationship with gorm in golang doesnt work
我有兩個表:
type Person struct {
ID int
FirstName string
LastName string
Functions []Function
}
type Function struct {
gorm.Model
Info string
Person Person
}
我創建這樣的表:
db.AutoMigrate(&models.Person{}, &models.Function{})
然后我初始化數據庫:
user := models.Person{
FirstName: "Isa",
LastName: "istcool",
Functions: []models.Function{{Info: "Trainer"}, {Info: "CEO"}},
}
db.Create(&user)
現在的問題是我的Person
表只有Firstname
和Lastname
列,而我的Function
表只有Info
列。 但是當我開始我的GET
請求時,我讓人們使用始終為空的列函數。
要查看代碼,請訪問我的GitHub 存儲庫
終於找到答案了!! 問題是我必須使用的 GET 函數
db.Preload("Functions").Find(&[]models.Person{})
代替
db.Find(&[]models.Person{})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.