dpdp42233 2017-02-04 07:04
浏览 693

Golang Gorm时间数据类型转换

Situation:

I'm using a postgres database and have the following struct:

type Building struct {
ID        int `json:"id,omitempty"`
Name      string `gorm:"size:255" json:"name,omitempty"`
Lon       string `gorm:"size:64" json:"lon,omitempty"`
Lat       string `gorm:"size:64" json:"lat,omitempty"`
StartTime time.Time `gorm:"type:time" json:"start_time,omitempty"`
EndTime   time.Time `gorm:"type:time" json:"end_time,omitempty"`
}

Problem:

However, when I try to insert this struct into the database, the following error occurs:

parsing time ""10:00:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "0:00"" as "2006""}.

Probably, it doesn't recognize the StartTime and EndTime fields as Time type and uses Timestamp instead. How can I specify that these fields are of the type Time?

Additional information

The following code snippet shows my Building creation:

if err = db.Create(&building).Error; err != nil {
    return database.InsertResult{}, err
}

The SQL code of the Building table is as follows:

DROP TABLE IF EXISTS building CASCADE;
CREATE TABLE building(
  id SERIAL,
  name VARCHAR(255) NOT NULL ,
  lon VARCHAR(31) NOT NULL ,
  lat VARCHAR(31) NOT NULL ,
  start_time TIME NOT NULL ,
  end_time TIME NOT NULL ,
  PRIMARY KEY (id)
);
  • 写回答

1条回答 默认 最新

  • duanpanbo9476 2017-02-04 07:40
    关注

    As mentioned in "How to save time in the database in Go when using GORM and Postgresql?"

    Currently, there's no support in GORM for any Date/Time types except timestamp with time zone.

    So you might need to parse a time as a date:

    time.Parse("2006-01-02 3:04PM", "1970-01-01 9:00PM")
    
    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?