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

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

戈姆屬于不返回關系

戈姆屬于不返回關系

Go
慕森王 2022-09-26 15:52:49
我有這些模型    type Message struct {    gorm.Model    UserID  uint `json:"userId"`    User    userModels.User    Content string `json:"content"`}type Receiver struct {    gorm.Model    UserID    uint `json:"userId"`    User      userModels.User    FirstName string `json:"firstName"`    LastName  string `json:"lastName"`    Email     string `json:"email"`    Address   string `json:"address"`    Phone     string `json:"phone"`}type Delivery struct {    gorm.Model    Message      Message   `json:"message"`    MessageID    uint      `json:"messageId"`    Receiver     Receiver  `json:"Receiver"`    ReceiverID   uint      `json:"receiverId"`    DeliveryDate time.Time `json:"deliveryDate"`    Sent         bool      `json:"sent"`}我在數據庫中插入了一些具有特定消息ID和接收器ID的傳遞。當我查詢某個傳遞時,我希望獲得 ID 為 MessageID 的相關消息,以及 id 為接收器 ID 的接收器。但是我得到的是一個同時包含空消息和接收方對象的傳遞。這就是我得到的: {            "ID": 2,            "CreatedAt": "2021-08-26T04:44:33.366628+04:30",            "UpdatedAt": "2021-08-26T04:44:33.366628+04:30",            "DeletedAt": null,            "message": {                "ID": 0,                "CreatedAt": "0001-01-01T00:00:00Z",                "UpdatedAt": "0001-01-01T00:00:00Z",                "DeletedAt": null,                "userId": 0,                "User": {                    "ID": 0,                    "CreatedAt": "0001-01-01T00:00:00Z",                    "UpdatedAt": "0001-01-01T00:00:00Z",                    "DeletedAt": null,                    "firstName": "",                    "lastName": "",                    "email": "",                    "password": "",                    "country": "",                    "dateOfBirth": "0001-01-01T00:00:00Z",                    "rank": "",                    "gender": "",                    "plan": ""                },                "content": ""            },      雖然我希望它返回消息,其中包含我指定的消息Id以及接收器
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

When I query that certain Delivery


你好?


如果使用預加載 db.Preload("Message").Preload("Receiver").First(&d3)


您將獲得接收器和消息的指示,例如


如果不使用預加載,您將獲得包含空消息和接收方對象的傳遞。


如果只需要消息 Id 和接收方 ID,則應將 更改為 和Message      Message Message      *Message Receiver     ReceiverReceiver     *Receiver


如果您使用預加載 ->與上述相同


如果不使用預加載 ->則接收方和消息將為零


以下是所有測試代碼和調試信息:


type User3 struct {

    gorm.Model

    Name      string

}

type Message struct {

    gorm.Model

    UserID  uint `json:"userId"`

    User    *User3

    Content string `json:"content"`

}


type Receiver struct {

    gorm.Model

    UserID    uint `json:"userId"`

    User      *User3

    FirstName string `json:"firstName"`

    LastName  string `json:"lastName"`

    Email     string `json:"email"`

    Address   string `json:"address"`

    Phone     string `json:"phone"`

}


type Delivery struct {

    gorm.Model

    Message      *Message   `json:"message"`

    MessageID    uint      `json:"messageId"`

    Receiver     *Receiver  `json:"Receiver"`

    ReceiverID   uint      `json:"receiverId"`

    DeliveryDate time.Time `json:"deliveryDate"`

    Sent         bool      `json:"sent"`

}

func main() {

    db := config.CreateMysql()

    db.AutoMigrate(Delivery{})

    d := Delivery{Message: &Message{User: &User3{Name: "a"}, Content: "hello b~"}, Receiver: &Receiver{User: &User3{Name: "b"}}, DeliveryDate: time.Now(), Sent: false}

    db.Create(&d)

    d2 := Delivery{}

    db.First(&d2)

    fmt.Printf("%v\n", d2)

    d3 := Delivery{}

    db.Preload("Message").Preload("Receiver").First(&d3)

    fmt.Printf("%v\n", d3)

}

在此處輸入圖像描述在此處輸入圖像描述

ps:以上用戶為零,如果要預加載嵌套關聯,請使用以下方法:https://gorm.io/docs/preload.html.Preload("Message.User")


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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