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

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

GORM 從兩個表中獲取一對一關系的數據

GORM 從兩個表中獲取一對一關系的數據

Go
胡子哥哥 2022-09-26 14:41:29
我的Go應用程序上有這兩個結構type Customer struct {    ID             uint       `json: "id" gorm:"primary_key"`    Name           string     `json: "name"`    AddressId      int        `json: "addressId"`    Address        Address    `json: "address"`}type Address struct {    ID        uint   `json: "id" gorm:"primary_key"`    ZipCode   string `json: "zipCode"`    StreetOne string `json: "streetOne"`    StreetTwo string `json: "streetTwo"`    City      string `json: "city"`    State     string `json: "state"`    Number    string `json: "number"`}我在我的前端使用Angular,所以如果我不必提出兩個請求來獲得客戶,那么這將是非常實用的。我在這里搜索,但找不到一對一關系的示例,有沒有辦法使此查詢不僅獲得客戶數據,還獲得地址?func (u customer) GetCustomers(params string) ([]models.Customer, error) {    customers := []models.Customer{}    u.db.Preload("Addresses").Find(&customers)    return customers, nil}
查看完整描述

1 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

使用該函數時,可以向其傳遞要為其加載數據的字段的名稱。Preload

在你的例子中,它應該看起來像這樣(因為你在結構中的字段被命名為):CustomerAddress

u.db.Preload("Address").Find(&customers)

您可以查看文檔以獲取更多詳細信息。


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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