Gorm: DATETIME fields in MySQL converted to string instead of time.Time

Created on 26 Nov 2013  路  5Comments  路  Source: go-gorm/gorm

I'm using MySQL 5.6.10, and the DATETIME fields are throwing this error when following the same format from your example:

type User struct {
    Id          int64
    CreatedAt   time.Time
}

The error thrown is:

sql: Scan error on column index 2: unsupported driver -> Scan pair: []uint8 -> *time.Time

This is complaining that the field is actually a string. Using string as the type sends the string equivalent of the datetime

2013-07-25 22:48:23

I can workaround this by just casting it to a time.Time myself but you may want to fix this issue. The driver I'm using the is typical one https://github.com/go-sql-driver/mysql.

I've made sure the field is indeed of type DATETIME in my database.

Let me know if you need any other information.

Most helpful comment

Did you open mysql with parseTime option like this?

db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")

All 5 comments

Did you open mysql with parseTime option like this?

db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")

No news is good news, feel free to reopen it if still have any issue.

parseTime=True fixed the issue for me, thanks!.

It works!!!

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bramp picture bramp  路  3Comments

leebrooks0 picture leebrooks0  路  3Comments

sredxny picture sredxny  路  3Comments

alanyuen picture alanyuen  路  3Comments

rfyiamcool picture rfyiamcool  路  3Comments