Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

插入 MySQL time 类型的字段,sql语句是 0000-01-01 H:i:s,诱发 Error: year is not in the range [1, 9999] #2012

Closed
niluan304 opened this issue Jul 15, 2022 · 2 comments · Fixed by #3714
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. inactive

Comments

@niluan304
Copy link
Contributor

  1. ENV

    go version :go 1.18 windows/amd64

    GoFrame Version : v2.1.1

    Mysql : 5.7

    Mysql Drivers : v2.1.0

  2. Can this issue be re-produced with the latest release?

    yes

  3. What did you do?

    在 MySQL表里,我创建了time类型的字段:

    CREATE TABLE `c_table` (
        ...
      `begin_at` time DEFAULT NULL,
        ...
    );

    gf gen dao生成的package do结构体:

    type CTable struct {
    	g.Meta        `orm:"table:c_table, do:true"`
        ...
    	BeginAt       *gtime.Time  
        ...
    }

    我在新增数据时,像这样操作,出现了问题:

    func Add(ctx context.Context) (err error, data any) (err error){
        var m = dao.CTable.Ctx(ctx)
        ...
        
        beginAt := "14:39:51"
        
        m.Insert(do.CTable{
            ...
            BeginAt : gtime.New(beginAt),
            ...
        })
        return
    }

    我得到这样的 sql 语句,以及 error

    INSERT INTO `c_table`(`begin_at`...) VALUES(...'0000-01-01 14:39:51') 
    Error: year is not in the range [1, 9999]: 0

    我顺便测试了下,修改年份,MySQL可以插入

    INSERT INTO `c_table`(`begin_at`...) VALUES(...'0001-01-01 14:39:51') 

    为了规避这个问题,我强行把 do.CTable.BeginAt 类型修改成了空接口

    type CTable struct {
    	g.Meta        `orm:"table:c_table, do:true"`
        ...
        BeginAt       any
        ...
    }

    插入数据时,存入手动转换的字符串:

    func Add(ctx context.Context) (err error, data any) (err error){
        var m = dao.CTable.Ctx(ctx)
        ...
        
        datetime := "2022-07-15 15:16:14"
        
        m.Insert(do.CTable{
            ...
            BeginAt : gtime.New(datetime).Layout("15:04:05"),
            ...
        })
        return
    }

    虽然可以跑,但我觉得不应该修改 package do的内容,也不优雅..

@Tiller-Mu
Copy link

Tiller-Mu commented Jul 19, 2022

golang time的0时刻是:0001-01-01 00:00:00。所以估计是gtime包有问题。

        beginAt := "14:39:51"
	aa := gtime.New(beginAt)
	fmt.Println(aa.ISO8601())

得到结果是:
0000-01-01T14:39:51+08:00

	aa := gtime.New(0)
	fmt.Println(aa.ISO8601())

得到的结果是:
0001-01-01T00:00:00+00:00

@gqcn gqcn added bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. labels Jan 18, 2023
@gqcn
Copy link
Member

gqcn commented Jan 18, 2023

新版本已经修复了这个问题,请使用最新版本再试试。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it. done This issue is done, which may be release in next version. inactive
Projects
None yet
3 participants