不支持的扫描,将 driver.Value 类型 []uint8 存储到类型 *time.Time 中。

huangapple go评论106阅读模式
英文:

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

问题

我在查询用户时遇到了困难,用户的定义如下:

type User struct {
    ID           int       `db:"id" json:"id"`            
    UserName     string    `db:"username" json:"username"` 
    Email        string    `db:"email" json:"email"`
    CreatedAt    time.Time `db:"created_at" json:"created_at"`
    StatusID     uint8     `db:"status_id" json:"status_id"`
    Deleted      uint8     `db:"deleted" json:"deleted"`
    ... 
}

而在MariaDB中,表的定义如下:

+--------------+------------------+------+-----+-------------------+----------------+
| Field        | Type             | Null | Key | Default           | Extra          |
+--------------+------------------+------+-----+-------------------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| username     | varchar(50)      | NO   |     | NA                |                |
| email        | varchar(255)     | NO   |     | NULL              |                |
| created_at   | datetime         | NO   |     | CURRENT_TIMESTAMP |                |
| status_id    | tinyint(1)       | NO   |     | 0                 |                |
| deleted      | tinyint(1)       | NO   |     | 0                 |                |
...              

然而,当我进行查询时,出现了以下错误:

sql: Scan error on column index 3: unsupported Scan, storing driver.Value type []uint8 into type *time.Time

尽管表中有一些行数据。我还尝试将created_at的类型更改为timestamp,但仍然出现相同的错误。

所以我不知道出了什么问题?我该如何解决?

**附注:**尽管我的问题的答案与这个问题相同,但这里的上下文不同(使用的是sqlx而不是go-sql-driver/mysql)。而且由于这里的错误是主题,对于那些在谷歌上搜索相同错误的人来说,这可能更容易搜索到。因此,也许值得将其作为一个单独的问题保留。

英文:

I have difficulty querieing for users, which is defined as:

type User struct {
	ID           int       `db:"id" json:"id"`            
	UserName     string    `db:"username" json:"username"` 
	Email        string    `db:"email" json:"email"`
	CreatedAt    time.Time `db:"created_at" json:"created_at"`
	StatusID     uint8     `db:"status_id" json:"status_id"`
	Deleted      uint8     `db:"deleted" json:"deleted"`
... 
}

And the table in MariaDB is defined as:

+--------------+------------------+------+-----+-------------------+----------------+
| Field        | Type             | Null | Key | Default           | Extra          |
+--------------+------------------+------+-----+-------------------+----------------+
| id           | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| username     | varchar(50)      | NO   |     | NA                |                |
| email        | varchar(255)     | NO   |     | NULL              |                |
| created_at   | datetime         | NO   |     | CURRENT_TIMESTAMP |                |
| status_id    | tinyint(1)       | NO   |     | 0                 |                |
| deleted      | tinyint(1)       | NO   |     | 0                 |                |
...              |

However when I query it like:

func GetUsers(c *gin.Context) {
	var users []model.User
	err := shared.Dbmap.Select(&users, "SELECT * FROM user")

	if err == nil {
		c.JSON(200, users)
	} else {
		fmt.Println("%v \n", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "no user(s) in the table or problem in the query"})
	}

	// curl -i http://127.0.0.1:8080/api/v1/users
}

I get this error:

sql: Scan error on column index 3: unsupported Scan, storing driver.Value type []uint8 into type *time.Time

while there are some rows in the table.

I have also tried created_at as timestamp but still get the same error.

So I'm left clueless as what wrong here? How can I fix it?

P.S. Though my question turned out to have the same answer as this but here the context is different (sqlx instead of go-sql-driver/mysql). Also since here the error is the subject it probably more searchable for people who google the same error. So perhaps this worth keeping as a separate question.

答案1

得分: 223

好的,我找到了解决方案,感谢这个答案。
问题是通过在数据库映射器中添加?parseTime=true来解决的。像这样:

db, err := sqlx.Connect("mysql", "myuser:mypass@tcp(127.0.0.1:3306)/mydb?parseTime=true")
英文:

Alright, I found the solution, thanks this answer.
The problem goes by adding ?parseTime=true to the db mapper. Like this:

db, err := sqlx.Connect("mysql", "myuser:mypass@tcp(127.0.0.1:3306)/mydb?parseTime=true")

答案2

得分: 23

在我的情况下,将代码更改为:

db, err := sql.Open("mysql", "root:@/?parseTime=true")

解决了问题。

英文:

In my case, changing to

db, err := sql.Open("mysql", "root:@/?parseTime=true")

resolved problem.

答案3

得分: 1

在我的情况下,我遇到了类似的问题,但是对于我的特定用例,它是一个包含了一个无法工作的选择(select)的结构体,因为我只需要选择数据,所以我简单地进行了以下操作:

将数据类型从time.Time更改为string

这解决了我的问题,我能够从数据库中获取数据。
也许这不是最好的解决方案,但这是对我有效的方法。

Karlom先生的解决方案对我也有效,这只是一种替代方法。

英文:

In my case, I had a similar problem but for my particular usecase, it was a struct with a select which didn't work, since I only needed to select data, then I proceeded to simply:

change the data type from time.Time to string

This solved my problem, and I was able to get the data from the database.
So perhaps it is not the best solution, but this is what worked for me.

Mr Karlom's solution also worked for me, this is just an alternative method.

答案4

得分: 0

如果你正在使用gorm包,可以尝试以下解决方案:

db, err := gorm.Open("mysql", "root:root@tcp(localhost:3306)/db?parseTime=true")

这将修复该问题。

英文:

Another Solution if you are using gorm pkg

db, err := gorm.Open("root:root@tcp(localhost:3306)db?parseTime=true"), &gorm.Config{})

This will fix the issue

huangapple
  • 本文由 发表于 2017年7月12日 00:45:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/45040319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定