zoukankan      html  css  js  c++  java
  • GORM对实现datetime和date类型时间

    model 定义

    type Plan struct {
    	Id int `gorm:"primary_key"`
    	Title string `gorm:"column:title;not null;size:128"`
    	Start time.Time  `gorm:"type:date;column:start"`
    	End time.Time  `gorm:"type:date;column:end"`
    	Created   time.Time `gorm:"autoCreateTime;column:created;type:datetime"`
    	IsDelete int `gorm:"column:is_delete;default:1"`
    }
    

      相比官网的例子,增加了type属性

    执行:

    	db.AutoMigrate(&Plan{})
    

    结果:

     

     建议把类型改为string

    type Plan struct {
    	Id int `gorm:"primary_key"`
    	User int `gorm:"column:user;not null"`
    	Title string `gorm:"column:title;not null;size:128"`
    	Start string `gorm:"type:date;column:start"`
    	End string `gorm:"type:date;column:end"`
    	Created   time.Time `gorm:"autoCreateTime;column:created;type:datetime"`
    	IsDelete int `gorm:"column:is_delete;default:1"`
    }
    

      这样gin处理前端发过来的时间比较友好,不然会出现这样的报错

    parsing time "2019-09-06" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "T"

     

  • 相关阅读:
    Linux查看系统信息
    pgrep 和 pkill 使用小记
    linux下json库的编译及例程
    Epoll 实例
    gcc中的内嵌汇编语言
    BZOJ 1053: [HAOI2007]反素数ant
    2018.7.15模拟赛
    BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊
    BZOJ 4241: 历史研究
    LUOGU P2365 任务安排
  • 原文地址:https://www.cnblogs.com/qinghuaL/p/15231819.html
Copyright © 2011-2022 走看看