Gorm属于不return关系

Gorm belongs to does not return relations

我有这些模型

    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"`
}

我用特定的 MessageID 和 ReceiverID 在数据库中插入了一些 Delivery。当我查询某个 Delivery 时,我希望获得 ID 为 MessageID 的相关消息和一个 ID 为 ReceiverID 的 Receiver。 但我得到的是一个包含空 Message 和 Receiver 对象的 Delivery。

这是我得到的:

 {
            "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": ""
            },
            "messageId": 2,
            "Receiver": {
                "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": ""
                },
                "firstName": "",
                "lastName": "",
                "email": "",
                "address": "",
                "phone": ""
            },
            "receiverId": 1,
            "deliveryDate": "2021-08-25T05:18:27.950457+04:30",
            "sent": false
        }

虽然我希望它 return 带有我指定的 messageId 的消息以及接收者

When I query that certain Delivery

你好吗?

如果您使用 Preload db.Preload("Message").Preload("Receiver").First(&d3)

你会得到Receiver的deltial和消息

如果你不使用 Preload 你得到的是一个包含空消息和接收者对象的交付。

如果只需要messageId和receiverID 您应该将 Message Message 更改为 Message *Message 并将 Receiver Receiver 更改为 Receiver *Receiver

如果你使用 Preload -> 同上

如果你不使用 Preload -> Receiver 和 Message 将为 nil

以下是所有测试代码和调试信息:

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:上面的user是nil,如果要预加载嵌套关联,使用如下: .Preload("Message.User") https://gorm.io/docs/preload.html