自動遷移時發現結構的無效字段
問題內容
當我嘗試根據我的結構自動遷移表時,出現此錯誤,我不知道為什麼會出現此錯誤
failed to parse value &models.model{id:0x0, createdat:time.date(1, time.january, 1, 0, 0, 0, 0, time.utc), updatedat:time.date(1, time.january, 1, 0, 0, 0, 0, time.utc), deletedat:, dogdata:[]models.dogdata(nil)}, got error invalid field found for struct github.com/dog-page/models.model's field dogdata: define a valid foreign key for relations or implement the valuer/scanner interface
我是 golang 語言和 gorm 的新手,特別是在為原始 json 資料創建結構時,這裡是我使用 gorm 的結構的意圖:
type Model struct { ID uint `gorm:"primarykey:id" json:"id:_id"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` DeletedAt *time.Time `gorm:"column:deleted_at" json:"deleted_at"` DogData []DogData } type DogData struct { DogDataID uint Name string `json:"name"` Life_Span string `json:"life_span"` Temperaments string `json:"temperament"` Weight datatypes.JSON `json:"weight"` Height datatypes.JSON `json:"height"` Image datatypes.JSON `json:"image"` } type Weight struct { Imperial string `json:"imperial"` Metric string `json:"metric"` } type Height struct { Imperial string `json:"imperial"` Metric string `json:"metric"` } type Image struct { URL string `json:"url"` }
解決方法
模型的欄位 dogdata:為關係定義有效的外鍵
模型與 dogdata 有一對多關係, gorm 無法辨識外鍵。
您需要指定在 dogdata
中儲存 model.id
的欄位(gorm 預設尋找 modelid),
type model struct { id uint `gorm:"primarykey:id" json:"id:_id"` ... dogdata []dogdata } type dogdata struct { modelid uint dogdataid uint ... } //create table `dog_data` (`model_id` integer,`dog_data_id` integer,`name` text,`life_span` text,`temperaments` text,constraint `fk_models_dog_data` foreign key (`model_id`) references `models`(`id`))
或如果您想使用另一列而不是使用foreignkey標記指定該列
type model struct { id uint `gorm:"primarykey:id" json:"id:_id"` ... dogdata []dogdata `gorm:"foreignkey:dogdataid"` } type dogdata struct { dogdataid uint ... } //create table `dog_data` (`model_id` integer,`dog_data_id` integer,`name` text,`life_span` text,`temperaments` text,constraint `fk_models_dog_data` foreign key (`dog_data_id`) references `models`(`id`))
不確定用例,但我認為 dogdata
可以簡化為,完全刪除 model
結構,因為它與 gorm.model
相同
type DogData struct { gorm.Model Name string `json:"name"` ... }
以上是自動遷移時發現結構的無效字段的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...
