postgresql - 使用 GORM 保存时间戳适用于 SQLite,但不适用于 Postgres
问题描述
这是我的模型:
type Event struct {
SessionId string `gorm:"type:uuid;primary_key;not null"`
Received int64 `gorm:"type:timestamp;primary_key;not null"`
EventType EventType `gorm:"primary_key;not null"`
UserId string `gorm:"type:uuid"`
GameId string
Story string
}
如果我使用 SQLite,“接收”时间戳将正确存储为 Unix 时间戳。但是,使用 Postgres 驱动程序,我得到:
pq: date/time field value out of range: "1593187082040"
解决方案
原来这type:timestamp
需要 time.Time 而不是 int64。为了解决这个问题,我必须将字段类型更改为 time.Time 并解析时间戳。但是,由于时间戳值以“ms”为单位,因此在 Go 中解析它有点棘手:
time.Unix(0, 1593187082040*int64(time.Millisecond))
推荐阅读
- javascript - 将服务注入 Angular 中的静态方法
- javascript - 视频每次循环都卡顿?
- python - Python列表中元素的总和大于任何数字
- python-3.x - 如何使用本地 python 安装在 ubuntu 14.04 上安装 pip
- mobile - 如何从 Svelte 中的 Materialize CSS 移动折叠导航栏?
- java - 编辑列表中的一个元素的 Api 端点
- javascript - 节点:无法通过 API 解析 JSON
- javascript - Javascript 中的灯泡无法打开
- java - JavaEE 和 Firebase admin sdk - setValueAsync 不将数据推送到实时 Firebase
- javascript - 使用不变性帮助程序更新功能不更新反应状态