gf icon indicating copy to clipboard operation
gf copied to clipboard

mysql 可空的datetime经过Scan方法之后,值不等于nil

Open itltf512116 opened this issue 5 years ago • 5 comments

1. What version of Go and system type/arch are you using?

go 1.16.3 /1.16.5 windows/amd64

2. What version of GoFrame are you using?

v1.16.0

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

gf v1.16.4版本是没问题的,master分支有问题,目前发现c25f88293bc1fee9deefe24a6e9092f6af7a1dcb版本的commit 没问题,50ffaef33fd68e801cc9a680f5b5f3749a1902c6 这个commit 存在问题

4. What did you do?

1. create table

CREATE TABLE test_date(
	id int not null PRIMARY KEY,
	dt datetime null 
)

INSERT into test_date(id,dt)values(1,null);

2. gf gen dao

3. go run main.go


import (
	"fmt"
	"gf-demo2/app/dao"
	"gf-demo2/app/model"
)

func main() {
	var td model.TestDate
	err2 := dao.TestDate.WherePri("id", 1).Scan(&td)
	fmt.Printf("date:%#v isNil:%v err:%v\n", td.Dt, td.Dt == nil, err2)
}

5. What did you expect to see?

td.Dt ==nil

6. What did you see instead?

itltf512116 avatar Jul 09 '21 02:07 itltf512116

@itltf512116 如果数据库的时间字段为null,那么读取到ORM中的对应时间对象可以使用IsZero来判断空值,不能使用nil判断。

gqcn avatar Aug 03 '21 14:08 gqcn

不是要判断它是不是nil,而是查询出来再save时,报错了 @gqcn

itltf512116 avatar Aug 04 '21 03:08 itltf512116

这个struct里的对应字段类型是指针,应该是nil才对吧?如果不是nil,修改了实体上其他字段的值,调用save时,sql执行报错

itltf512116 avatar Aug 04 '21 03:08 itltf512116

这个struct里的对应字段类型是指针,应该是nil才对吧?如果不是nil,修改了实体上其他字段的值,调用save时,sql执行报错

报什么错?

gqcn avatar Aug 04 '21 05:08 gqcn

gtime 是空时,insert 会格式化为 '',在 pg 情况下,会提示

pq: invalid input syntax for type timestamp with time zone: ""

ZhangHang avatar Aug 24 '21 08:08 ZhangHang