我有这些模型
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以及接收器
HUX布斯
相关分类