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

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

通過 Gorm 查詢模型

通過 Gorm 查詢模型

Go
蝴蝶刀刀 2022-12-05 16:39:48
這是我的律師模型type Lawyer struct {        ID            uint                 `gorm:"primaryKey" json:"id"`        FirstName     string               `gorm:"type:varchar(100) not null" json:"first_name"`        LastName      string               `gorm:"type:varchar(100) not null" json:"last_name"`        FullName      string               `gorm:"->;type:text GENERATED ALWAYS AS (concat(first_name,' ',last_name)) VIRTUAL;" json:"full_name"`        LocationID    uint                 `gorm:"not null" json:"location_id"`        Location      Location             `gorm:"foreignKey:location_id" json:"location"`        Email         string               `gorm:"unique;not null" json:"email"`        Phone         string               `gorm:"type:varchar(100);not null" json:"phone"`        Password      string               `gorm:"type:varchar(100);not null" json:"password"`        ImageURL      string               `gorm:"type:text" json:"image_url"`        Education     string               `gorm:"not null" json:"education"`        Experience    uint                 `gorm:"not null" json:"experience"`        PracticeAreas []LawyerPracticeArea `gorm:"foreignKey:LawyerID" json:"practice_areas"`        CreatedAt time.Time `gorm:"" json:"created_at"`        UpdatedAt time.Time `gorm:"" json:"updated_at"`    }最后這是我的 PracticeArea 模型type PracticeArea struct {    ID     uint   `gorm:"primaryKey" json:"practice_area_id"`    Name   string `gorm:"not null" json:"name"`    AvgFee string `gorm:"not null" json:"avg_fee"`}我正在通過這個查詢我的律師模型:-result := db.Preload(clause.Associations).Find(&lawyer)此結果也包含所有 Lawyers 和 LawyerPracticeAreas 數據,但不包含來自 LawyerPracticeAreas 內的 PracticeArea 表的數據。Lawyer 和 PracticeArea 具有多對多關系,而 LawyerPracticeAreas 就是該表。
查看完整描述

1 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

根據文檔

clause.Associations不會預加載嵌套關聯,但您可以將它與嵌套預加載一起使用...

在您的情況下,要加載所有內容,甚至是嵌套超過一層的關聯,您可以這樣做:

result := db.Preload("Location").Preload("PracticeAreas.PracticeArea").Find(&lawyer)


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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