相关疑难解决方法(0)

使用GORM和Postgresql时如何在Go中节省数据库时间?

我目前正在解析时间字符串并将其保存到数据库(Postgresql):

event.Time, _ := time.Parse("3:04 PM", "9:00 PM")
// value of event.Time now is: 0000-01-01 21:00:00 +0000 UTC
db.Create(&event)
Run Code Online (Sandbox Code Playgroud)

它给了我这个错误: pq: R:"DateTimeParseError" S:"ERROR" C:"22008" M:"date/time field value out of range: \"0000-01-01T21:00:00Z\"" F:"datetime.c" L:"3540"

event.Time?????的类型是time.Time.

我还尝试将event.Time的类型设置为字符串并在 postgresql 中使用时间数据类型:

type Event struct {
  Time string `gorm:"type:time
}
Run Code Online (Sandbox Code Playgroud)

但是现在在数据库中获取记录时出现错误:

sql: Scan error on column index 4: unsupported driver -> Scan pair: time.Time -> *string
Run Code Online (Sandbox Code Playgroud)

postgresql go go-gorm

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

go ×1

go-gorm ×1

postgresql ×1