golang - gorm 更新表的时候,总是把created_at字段也更新的,很奇怪。。。
本文介绍了golang - 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
解决方案
看图,我觉得你应该懂了
这篇关于golang - gorm 更新表的时候,总是把created_at字段也更新的,很奇怪。。。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文