gorm 更新表的时候,总是把created_at字段也更新的,很奇怪。。。

这是我的表定义

type Notice struct {
    Id          int  `gorm:"primary_key"`
    Title        string `gorm:"type:varchar(20);not null"`
    Content     string `gorm:"type:text;not null"`
    CreatedAt   time.Time `gorm:"not null"`
    UpdatedAt   time.Time `gorm:"not null"`
    AdminId     int `gorm:"not null"`
}
数据库定义:
Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
title varchar(20) NO NULL
content text NO NULL
created_at timestamp NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
updated_at timestamp NO 0000-00-00 00:00:00
admin_id int(11) NO NULL

我这么写:

db.Model(n).Updates(map[string]interface{}{"title": n.Title, "content": n.Content, "updated_at": time.Now()})

每次都更新了created_at

clipboard.png

阅读 19.8k
1 个回答

看图,我觉得你应该懂了

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题