I know that gorm.db initialize the data of "created_at", "updated_at", "deleted_at" automatically
but i have a problem with function "db.Save(&obj)" that update the field "updated_at" but reset the field "created_at" to "0001-01-01T00:00:00Z"
Here's my code:
model.go
type CommentId struct {
Id uint64 `json:"id" sql:"AUTO_INCREMENT"`
}
type CommentData struct {
Message string `json:"message"`
ObjectModel string `json:"objectModel"`
ObjectId uint `json:"objectId"`
SpaceId uint `json:"spaceId"`
}
type Comment struct {
CommentId
CommentData `json:"data"`
Trace `json:"trace"`
}
router.go
func PutComment(db gorm.DB, router *gin.Engine) {
// PUT /comment
// Update comment data by id
router.PUT("/comment/:id", func(c *gin.Context) {
_id := c.Param("id")
if id, err := strconv.ParseUint(_id, 10, 64); err == nil {
var commentData model.CommentData
if err := c.BindJSON(&commentData); err == nil {
comment := &model.Comment{
CommentData: commentData,
CommentId: model.CommentId {Id: id},
}
if err := checkDataComment(comment.CommentData); err {
checkComment := &model.Comment{
CommentData: commentData,
CommentId: model.CommentId {Id: id},
}
if err := db.First(checkComment).Error; err == nil {
db.Save(&comment) // reset "created_at field"
c.JSON(http.StatusOK, comment)
} else {
c.AbortWithStatus(http.StatusNotFound)
}
} else {
c.AbortWithStatus(http.StatusBadRequest)
}
}
} else {
log.Print(err)
c.AbortWithError(http.StatusBadRequest, err)
}
})
}
Exactly the same issue. I think that the save method should never overwrite the created_at field, now I have to manually get the old row and manually set the created_at field.
Hi @ali-abdalla , @alecha
Could you follow this guide to submit an executable script to confirm this issue?
https://github.com/jinzhu/gorm/blob/master/CONTRIBUTING.md
As in my daily use, haven't seen this problem?
I've created a Gist, hope it's clear.
@alecha I see, to me, I think we should keep the ability to for users to update any columns, including CreatedAt.
For your example, I have few suggestions:
db.First(&user, id)
// decode json to user
db.Save(&user)
db.Select("FirstName", "LastName").Save(&user)
db.Model(&user).Updates(attrs)
Using
db.Model(User{ID: u.ID}).Updates(u)
seems to solve the issue, but in case I want to set an empty string for a field it won't work right?
Yes, as there are no different for: User{}, User{FirstName: ""}, so won't update empty string
There is an issue explained this, and have some solutions for it.
Another and better way is Omit field:
db.Omit("created_at", "deleted_at").Save(&user)
i just want to confirm. if have simple struct model like this:
MCell struct {
gorm.Model
Name string `json:"name"`
IP string `json:"ip"`
}
and i want to save after updating some of value of an cell object:
cell := MCell{}
db.FirstOrCreate(&cell, MCell{Name: "ASC1-B"})
cell.IP = "192.168.5.31"
db.Save(&cell)
and then, why all field value (except IP) reset?
name is blank, and created_at is zero date-time. and i'm passing cell.Name = cell.Name and then, error will happen(vet error)
Most helpful comment
Another and better way is Omit field:
db.Omit("created_at", "deleted_at").Save(&user)