问题描述

unsupported Scan, storing driver.Value type []uint8 into type *time.Time

问题原因

Mysql的字段类型为time时,即使在dns中添加parseTime=true参数,查询数据也会报这个错,后来我改成datetime后可以正常查询,这个是解析的问题还是我不应该将MySqltime类型映射为golangtime.Time

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: li-jin-gou

https://github.com/go-gorm/datatypes/blob/master/time.go 用这个~

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: congjunhua

兄弟解决了吗

Comment From: ZoftTy

@congjunhua

兄弟解决了吗

上面那位同学已经给出了解决方法啦

https://github.com/go-gorm/datatypes/blob/master/time.go 用这个~

import (
    "gorm.io/datatypes"
)


type TestModel struct {
    TestTime datatypes.Time `gorm:"column:test_time"`
}

如果你是用 gorm gen 来生成模型或许可以这样子

dataMap := map[string]func(columnType gorm.ColumnType) (dataType string){
"time": func(columnType gorm.ColumnType) (dataType string) { return "datatypes.Time" },
}

// 要先于`ApplyBasic`执行
g.WithDataTypeMap(dataMap)

Comment From: congjunhua

@congjunhua

兄弟解决了吗

上面那位同学已经给出了解决方法啦

https://github.com/go-gorm/datatypes/blob/master/time.go 用这个~

```go import ( "gorm.io/datatypes" )

type TestModel struct { TestTime datatypes.Time gorm:"column:test_time" } ```

如果你是用 gorm gen 来生成模型或许可以这样子

```go dataMap := map[string]func(columnType gorm.ColumnType) (dataType string){ "time": func(columnType gorm.ColumnType) (dataType string) { return "datatypes.Time" }, }

// 要先于ApplyBasic执行 g.WithDataTypeMap(dataMap) ```

好吧,还是要引gorm.io/datatypes这个包。