1、背景
- 使用gorm链接pg数据库
- model通过grom的gen生成
- 字段为有time.time类型
- 需要把公共的字段进行抽离,统一在一个ops方法中进行添加
2、遇见的坑
- pgConn.Create时,结构体中没有添加的字段就会使用默认值,比如update_time就会使用0000-00-00 00:00:01 这个字段本身应该是null的
- 字段不使用默认值按照文档找到,可以添加”-“来忽略这个字段,添加完成后发现在更新操作时,这个字段也是被忽略的
UpdateTime string `gorm:"column:update_time;comment:最后更新时间;-" json:"update_time"`
3、解决办法
- 时间使用*time.time类型
UpdateTime *time.time `gorm:"column:update_time;comment:最后更新时间" json:"update_time"`
- reflact时
value := time.Now() reflectValue.FieldByName(key).Set(reflect.ValueOf(&value))