英文:
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时,我期望得到相应的Message和具有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
}
而我期望它返回我指定的messageId的Message和具有ReceiverID的Receiver。
英文:
I have these models
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"`
}
I insert some Delivery in the database with certain MessageID and ReceiverID. When I query that certain Delivery I expect to get the regarding message with the id of MessageID, and a Receiver of id ReceiverID.
But what I get is a Delivery with both empty Message and Receiver objects.
this is what I get:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
{
"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
}
<!-- end snippet -->
While I expect it to return the message with the messageId I specified and also the receiver
答案1
得分: 2
当我查询特定的交付时,你好。如果你使用Preload,你将获得接收者和消息的详细信息。如果你不使用Preload,你将得到一个包含空消息和接收者对象的交付对象。如果你只需要消息ID和接收者ID,你应该将Message Message
更改为Message *Message
,将Receiver Receiver
更改为Receiver *Receiver
。如果你使用Preload,结果与上述相同。如果你不使用Preload,接收者和消息将为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)
}
上述用户为nil,如果你想预加载嵌套关联,请使用以下方式:.Preload("Message.User")
。详细信息请参考:https://gorm.io/docs/preload.html
英文:
When I query that certain Delivery
how do you do?
if you uses Preload
db.Preload("Message").Preload("Receiver").First(&d3)
you will get the deltial of Receiver and message like
if you don't uses Preload
what you get is a Delivery with both empty Message and Receiver objects.
if you only need messageId and receiverID
you should change the Message Message
to Message *Message
and Receiver Receiver
to Receiver *Receiver
if you uses Preload -> same to above
if you don't uses Preload -> Receiver and Message will be nil
follow is all test code and debug info:
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: above user is nil, if you want preload nested associations, uses follow:
.Preload("Message.User")
https://gorm.io/docs/preload.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论