请问下gorm在定义模型关联时,返回的数据问题
1. 运行环境
go1.18.4 darwin/amd64
2. 问题描述?
G02教程16章,在创建话题成功后,返回了
{
"code": 200,
"data": {
"id": 1,
"title": "随意生成的标题修改1",
"content": "随意生成的标题修改,随意生成的标题修改1",
"user_id": 7,
"category_id": 1,
"user": { // 这里返回user字段不全
"created_at": "0001-01-01 00:00:00",
"updated_at": "0001-01-01 00:00:00"
},
"category": { // 这里返回category字段不全
"created_at": "0001-01-01 00:00:00",
"updated_at": "0001-01-01 00:00:00"
},
"created_at": "2022-10-08 16:46:45",
"updated_at": "2022-10-19 18:39:47"
}
}
Model代码
type Topic struct {
models.BaseModel
Title string `json:"title,omitempty"`
Content string `json:"content,omitempty"`
UserID int64 `json:"user_id,omitempty"`
CategoryID int64 `json:"category_id,omitempty"`
User user.User `json:"user"`
Category category.Category `json:"category"`
models.CommonTimestampsField
}
controller代码
func (ctrl *TopicsController) Store(c *gin.Context) {
request := requests.TopicRequest{}
if ok := requests.Validate(c, &request, requests.TopicSave); !ok {
return
}
userModel := auth.User(c)
cid, _ := strconv.ParseInt(request.CategoryID, 10, 64)
topicModel := topic.Topic{
Title: request.Title,
Content: request.Content,
CategoryID: cid,
UserID: userModel.ID,
}
topicModel.Create()
if topicModel.ID > 0 {
response.Created(c, topicModel)
} else {
response.Abort500(c, "创建失败,请稍后尝试~")
}
}
3. 您期望得到的结果?
正常不是应该返回user/category的全部字段吗?或者不返回,因为没有预加载
请问这个种问题需要怎么处理?
不想去掉关联关系,但是在没有加载的情况下也不想展示无关字段
已经找到解决方案了,是在定义模型时,如果有结构体嵌套(也就是模型关联) 可以将关联的结构体设置成指针类型,包括上边说到的时间类型也设置成指针类型 这样在没有预加载的时候,关联的接口体对应的就是空指针,就会在json解析的时候被过滤掉,前提是需要设置
omitempty
标签