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

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

恐慌:反映:在 GORM .Create() 上調用 reflect.Value.Interface

恐慌:反映:在 GORM .Create() 上調用 reflect.Value.Interface

Go
楊__羊羊 2022-12-26 10:47:32
我是后端新手,我正在嘗試在表之間建立多對多關系。我使用這個 repo 來制作模型:https://github.com/harranali/gorm-relationships-examples/tree/main/many-to-many 我在 postgresql 中使用了 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:"many-to-many: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"`}連接到數據庫并自動遷移后:func init() {    config.Connect()    db = config.GetDB()    db.AutoMigrate(&models.Book{}, &models.AuthorsCard{})}我創建了 Function 來查看這種關系是如何工作的:func TestCreate() {var AuthorsCard = []models.AuthorsCard{        {            Age:         23,            Name:        "test",            YearOfBirth: 1999,            Biography:   "23fdgsdddTEST",        },    }    db.Create(&AuthorsCard)    var testbook = models.Book{        Title:       "Test",        Author:      "tst",        Description: "something",    }    db.Create(&testbook)    db.Model(&testbook).Association("AuthorsCard").Append(&AuthorsCard)} 但是得到了這個錯誤:恐慌:反映:在零值上調用 reflect.Value.Interface [恢復]恐慌:反映:在零值上調用 reflect.Value.Interface我該如何處理這個“空”問題并建立正確的關系?UPD:問題的第一部分與 GORM 的版本有關,在我將舊版本(github.com/jinzhu/gorm v1.9.16)更改為新版本(gorm.io/gorm v1.23.6)后反映出問題錯誤消失了。但是現在,當我想創建新書時,我得到了這個錯誤:/go/pkg/mod/gorm.io/driver/[email protected]/migrator.go:119 錯誤:沒有匹配引用表“authors_cards”的給定鍵的唯一約束(SQLSTATE 42830)[28.440ms] [行:0]創建表“book_authorscard”(“book_id”bigint,“authors_card_id”bigint,主鍵(“book_id”,“authors_card_id”),約束“fk_book_authorscard_authors_card”外鍵(“authors_card_id”)參考“authors_cards”(“id” ),CONSTRAINT "fk_book_authorscard_book" FOREIGN KEY ("book_id") REFERENCES "books"("id")) [GIN-debug] [WARNING] 創建一個已經附加了 Logger 和 Recovery 中間件的引擎實例。
查看完整描述

1 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

通過閱讀 Gorm v2 ( https://gorm.io/docs/v2_release_note.html ) 的發行說明,我認為您正在嘗試將 v2 功能與舊版本 (<v2) 一起使用。嘗試使用 Gorm 最新版本。



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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