这是我的表定义
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
看图,我觉得你应该懂了