根據(jù)php小編香蕉的介紹,GORM是一款流行的Go語(yǔ)言O(shè)RM庫(kù),它提供了便捷的數(shù)據(jù)庫(kù)操作方式。然而,GORM并不支持所有的數(shù)據(jù)類型,并且在處理某些數(shù)據(jù)類型時(shí)可能會(huì)出現(xiàn)錯(cuò)誤的架構(gòu)。這意味著在使用GORM時(shí),開發(fā)者需要注意使用支持的數(shù)據(jù)類型,并確保數(shù)據(jù)架構(gòu)正確,以避免潛在的問(wèn)題。雖然GORM功能強(qiáng)大,但這些限制需要開發(fā)者在使用時(shí)謹(jǐn)慎處理。
問(wèn)題內(nèi)容
gorm v1.25.1,我嘗試在 worker
、poster
和 job
模型上運(yùn)行 db.automigrate()
,但遇到 [錯(cuò)誤] 不支持的數(shù)據(jù)類型:&[]
。 worker 和 job 結(jié)構(gòu)應(yīng)該具有 many-to-many 關(guān)系
,而 poster 和 job 應(yīng)該具有 one-to-many
關(guān)系。工人和經(jīng)驗(yàn)、工人和偏好都應(yīng)該是 one-to-many
關(guān)系。請(qǐng)幫忙。
package model type experience struct { gorm.Model Company int `json:"company"` JobTitle string `json:"jobTitle"` WorkerID uint } type preference struct { gorm.Model JobTitle string `json:"JobTitle"` MinPay int `json:"minPay"` MaxPay int `json:"maxPay"` WorkerID uint } type Worker struct { gorm.Model Username string `gorm:"uniqueIndex;not null" json:"username"` ...more fields Experience []experience `json:"experience"` Preference []preference `json:"preference"` AppliedJobs []Job `gorm:"many2many:worker_jobs;" json:"appliedJobs"` } type Poster struct { gorm.Model Name string `gorm:"uniqueIndex;not null" json:"name"` Email string `gorm:"uniqueIndex;not null" json:"email"` Phone string `json:"phone"` JobsPosted []Job `json:"jobsPosted"` } type Job struct { gorm.Model Title string `gorm:"uniqueIndex;not null" json:"title"` ...more fields PosterID uint `json:"posterID"` }
登錄后復(fù)制
解決方法
我能夠通過(guò)以下代碼實(shí)現(xiàn)您所需要的。
請(qǐng)注意,為了演示,我通過(guò)僅包含關(guān)聯(lián)的相關(guān)字段來(lái)簡(jiǎn)化您的模型(結(jié)構(gòu))。如果我遺漏了一些值得一提的內(nèi)容,請(qǐng)隨時(shí)詢問(wèn),我會(huì)更新我的答案。
我先分享一下代碼,然后再詳細(xì)解釋。
package main import ( "gorm.io/driver/postgres" "gorm.io/gorm" ) type Worker struct { gorm.Model Username string `gorm:"uniqueIndex;not null" json:"username"` Posters []Poster `gorm:"many2many:workers_posters;joinForeignKey:postersId"` } type Poster struct { gorm.Model Name string `gorm:"uniqueIndex;not null" json:"name"` Email string `gorm:"uniqueIndex;not null" json:"email"` Phone string `json:"phone"` Workers []Worker `gorm:"many2many:workers_posters;joinForeignKey:workersId"` Jobs []Job } type Job struct { gorm.Model Title string `gorm:"uniqueIndex;not null" json:"title"` PosterID uint `json:"posterID"` } func main() { dsn := "host=localhost port=54322 user=postgres password=postgres dbname=postgres sslmode=disable" db, err := gorm.Open(postgres.Open(dsn)) if err != nil { panic(err) } db.AutoMigrate(&Worker{}, &Poster{}, &Job{}) }
登錄后復(fù)制
由于您的問(wèn)題同時(shí)涉及 many2many
和 one2many
關(guān)系,因此我將把我的答案分為兩部分。
many2many
關(guān)系
要實(shí)現(xiàn)這種關(guān)系,您必須在保存關(guān)聯(lián)實(shí)體的切片旁邊添加注釋 gorm:"many2many:workers_posters;joinforeignkey:workersid"
。受影響的字段是:
posters
結(jié)構(gòu)體中的 worker
字段
workers
結(jié)構(gòu)體中的 poster
字段
顯然,您必須根據(jù)要設(shè)置的字段更改 joinforeignkey
的值。
one2many
關(guān)系
這種關(guān)系甚至更簡(jiǎn)單,因?yàn)槟槐刂付ū仨氃?many2many
關(guān)聯(lián)中創(chuàng)建的聯(lián)接表(例如上面創(chuàng)建的 workers_posters
表)。在這里,您只需要做這兩個(gè)更改:
-
在
poster
結(jié)構(gòu)中添加 jobs []job
字段在
job
結(jié)構(gòu)中添加 posterid uint
字段
因此,如果您運(yùn)行 automigrate
方法,您將在數(shù)據(jù)庫(kù)中看到正確的表,并且所有外鍵都已正確設(shè)置。
請(qǐng)告訴我,謝謝!