I'm trying to get my updated post in to a struct in the below function
func UpdatePost(c *gin.Context) {
id := c.Param("id")
var body struct {
Title string
PostText string
Img string
}
c.Bind(&body)
var post models.Post
initializers.DB.Raw("UPDATE posts SET title = ?, post_text = ?, img = ? WHERE id = ?", body.Title, body.PostText, body.Img, id).Scan(&post)
c.JSON(http.StatusOK, gin.H{
"post": post,
})
}
My Post is being successfully updated in DB but even after using Scan(), my struct looks like this
"post": {
"ID": 0,
"title": "",
"postText": "",
"img": "",
"userName": "",
"likedBy": null,
"createdBy": 0,
}
What's the way to go here?
CodePudding user response:
Just Missed RETURNING * after WHERE id = ? like @rahmat commented. Leaving this here for anyone who's trying to find the correct syntax for gorm raw SQL builder in future