V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
dyllen
V2EX  ›  Go 编程语言

gorm 自动生成的模型,数据库数据类型时 date,模型生成的类型时 timt.Time,生成的查询变成了 datetime 格式的,怎么设置能查询正确?

  •  
  •   dyllen · 172 天前 · 765 次点击
    这是一个创建于 172 天前的主题,其中的信息可能已经有所发展或是发生改变。

    模型时 gorm 的 gen 工具自动生成的:

    Date      time.Time `gorm:"column:date;type:date;not null;comment:日期" json:"date"`
    

    生成的查询

    `date` = '2023-11-07 08:00:00' LIMIT 1
    

    要怎么设置才能生成日期格式时2023-11-07的正确查询?

    4 条回复    2023-11-08 15:42:37 +08:00
    gouguoyin
        1
    gouguoyin  
       172 天前
    你可以试试 https://github.com/golang-module/carbon#json-handling
    将 `Date` 类型设置为 `carbon.Date`
    pkoukk
        2
    pkoukk  
       172 天前
    dsn 里加上 parseTime=True&loc=Local
    body007
        3
    body007  
       171 天前
    直接用字符串呗,不一定非要传 time.Time 对象吧。

    db.Debug().Model(&Data{}).Where("date = ?", time.Now().Format(time.DateOnly)).Limit(5).Find(&data)
    dyllen
        4
    dyllen  
    OP
       171 天前
    @body007 之前用的自动生成查询的方法,参数得传 time.Time 对象,我另外写了一个,用字符串,和你一样的了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1418 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:13 · PVG 01:13 · LAX 10:13 · JFK 13:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.