dongzou9047 2017-07-11 16:45
浏览 732
已采纳

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

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 
", 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条回答 默认 最新

  • doubao6936 2017-07-11 17:07
    关注

    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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题