我想加载帖子,并且是作者ID和电子邮件,而无需加载任何其他内容。
它不需要急于加载,我只需要获取所有帖子,只包括作者ID和用户名
我的当前查询
posts := []interfaces.Post{}
db.Model("User").Find(&posts)
type Post struct{
gorm.Model
Title string
Body string
UserID uint
User User
}
type User struct{
gorm.Model
Username string
Email string
Password string
}
当前响应
{
"ID": 1,
"CreatedAt": "2021-01-09T19:11:42.063274-05:00",
"UpdatedAt": "2021-01-09T19:11:42.063274-05:00",
"DeletedAt": null,
"Title": "What does the fox say",
"Body": "whawhhwjg",
"UserID": 1,
"User": {
"ID": 1,
"CreatedAt": "2021-01-09T19:01:28.70267-05:00",
"UpdatedAt": "2021-01-09T19:01:28.70267-05:00",
"DeletedAt": null,
"Username": "12345",
"Email": "1112@gmail.com",
"Password": "$2a$04$T1841Dc52MwjSJ2PaPnTwuFASai6zkGw8WFcuQbO1fi9Nug7R3Iqq"
}
},
我正在寻找的响应
{
"ID": 1,
"CreatedAt": "2021-01-09T19:11:42.063274-05:00",
"UpdatedAt": "2021-01-09T19:11:42.063274-05:00",
"DeletedAt": null,
"Title": "What does the fox say",
"Body": "whawhhwjg",
"UserID": 1,
"User": {
"ID": 1,
"Username": "12345",
}
},
回首忆惘然
白衣非少年
相关分类