【发布时间】:2020-07-11 10:40:03
【问题描述】:
我正在编写一个使用 GORM ORM 与sqlite3 数据库通信的应用程序。现在问题是我需要将 UpdatedAt 列设为 unix 时间戳,因为另一个旧应用程序正在使用相同的数据库,它们应该是兼容的。
所以我尝试使用以下代码块在 BeforeUpdate 挂钩上使用 unix 时间戳更新 UpdatedAt 字段。
func (c *CartItems) BeforeUpdate() (err error) {
fmt.Println("----------------------------------------")
fmt.Println("BeforeUpdate")
fmt.Println( c )
c.UpdatedAt = time.Now().Unix()
fmt.Println("----------------------------------------")
return
}
但是当我运行代码查询时保持不变并且没有将时间戳添加到数据库中。
戈姆日志
----------------------------------------
BeforeUpdate
&{55 21 4 7 1585607114 1585607114 {0 [] [] [] {0 0 } 0 0} {0 0 0 0}}
----------------------------------------
[2020-03-31 04:30:02] [0.46ms] UPDATE "cart_items" SET "quantity" = 7, "updated_at" = '2020-03-31 04:30:02' WHERE "cart_items"."id" = 55
[1 rows affected or returned ]
[GIN] 2020/03/31 - 04:30:02 | 200 | 97.963597ms | 127.0.0.1 | POST "/cache/cart/21/item"
【问题讨论】: