亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GORM 不會創建 many2many 關聯

GORM 不會創建 many2many 關聯

Go
慕后森 2022-12-26 15:39:15
我有模型:type Book struct {    gorm.Model    Title       string `json:"title"`    Author      string `json:"author"`    Description string `json:"description"`    Category    string `json:"Category"`    Publisher   string `json:"publisher"`    AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard; "json:"authorscard"`}type AuthorsCard struct {    gorm.Model    Name        string `json:"name"`    Age         int    `json:"age"`    YearOfBirth int    `json:"year"`    Biography   string `json:"biography"`}在 db.AutoMigrate(&models.Book{}, &models.AuthorsCard{})我有一個創建函數:func TestCreate() {    var testbook = models.Book{        Title:  "Test",        Author: "tst",        AuthorsCard: []*models.AuthorsCard{            {                Age:         23,                Name:        "test",                YearOfBirth: 1999,                Biography:   "23fdgsdddTEST",            },        },        Description: "something",    }    db.Preload("AuthorsCard").Find(&models.Book{})    db.Create(&testbook)}盡管 AuthorsCard 中有一些數據,但我收到了以下回復:{        "ID": 78,        "CreatedAt": "2022-06-23T13:10:58.01629+03:00",        "UpdatedAt": "2022-06-23T13:10:58.01629+03:00",        "DeletedAt": null,        "title": "Test",        "author": "tst",        "description": "something",        "Category": "",        "publisher": "",        "authorscard": null    }如您所見,“authorscard”為空。如何將“authorscard”保存到數據庫?我使用了 Gorm + postgresql 并且我用郵遞員發送了一些請求,結果是一樣的 - 作者卡是空的
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

當以正確的順序調用代碼時,代碼可以正常工作:


func TestCreate() {

    db := getDB()

    db.AutoMigrate(&Book{}, AuthorsCard{})


    var testbook = Book{

        Title:  "Test",

        Author: "tst",

        AuthorsCard: []*AuthorsCard{

            {

                Age:         23,

                Name:        "test",

                YearOfBirth: 1999,

                Biography:   "23fdgsdddTEST",

            },

        },

        Description: "something",

    }


    // 1. Create your testbook.

    db.Create(&testbook)


    // 2. Store it into a variable:

    var b1 *Book

    db.Preload("AuthorsCard").Find(&b1)

    

    fmt.Println(b1.AuthorsCard[0].Age)

    fmt.Println(b1.AuthorsCard[0].Name)

    fmt.Println(b1.AuthorsCard[0].YearOfBirth)

    fmt.Println(b1.AuthorsCard[0].Biography)


}

印刷:


23 測試 1999 23fdgsdddTEST


此外,您的 JSON 導出可能會失敗,因為您將指針傳遞給 AuthorCard 并且在這些情況下編組并不總是正常工作。然而,GORM 在這方面做得很好。


靜態檢查在這里也給了我一些提示:


type Book struct {

    gorm.Model

    Title       string         `json:"title"`

    Author      string         `json:"author"`

    Description string         `json:"description"`

    Category    string         `json:"Category"`

    Publisher   string         `json:"publisher"`

    AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard" json:"authorscard"` // wrong space

}


查看完整回答
反對 回復 2022-12-26
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號