簡體   English   中英

golang中與gorm的一對多關系不起作用

[英]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表只有FirstnameLastname列,而我的Function表只有Info列。 但是當我開始我的GET請求時,我讓人們使用始終為空的列函數。

這是我的 GET 請求和我的數據庫的屏幕截圖

要查看代碼,請訪問我的GitHub 存儲庫

終於找到答案了!! 問題是我必須使用的 GET 函數

db.Preload("Functions").Find(&[]models.Person{})

代替

db.Find(&[]models.Person{})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM