Golang : Error reading timestamp with GORM or SQL driver
Problem :
It is common for application to read from database these days and sometimes a missing parameter in the database connection configuration can cause chaos. For instance, while attempting to read time stamp data from database, you get these funky errors :
(sql: Scan error on column index 2: unsupported driver -> Scan pair: []uint8 -> *time.Time)
and when you try to read time stamp from your database(MySQL, SQLite, etc), it becomes :
0001-01-01 00:00:00 +0000 UTC
Solution :
Make sure that your database connection is initialized with parseTime=true
parameter.
For example :
dbConn, err := gorm.Open("mysql", "username:password@tcp(xx.xx0.x65.xx4:3306)/dbname?charset=utf8&parseTime=true")
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+43.2k Golang : Convert []byte to image
+4.7k Linux/MacOSX : How to symlink a file?
+10.6k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+6.9k Golang : How to solve "too many .rsrc sections" error?
+16k Golang : How to reverse elements order in map ?
+11.7k How to tell if a binary(executable) file or web application is built with Golang?
+5.6k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+5.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+20.2k Golang : Determine if directory is empty with os.File.Readdir() function
+14.2k Golang : Fix image: unknown format error
+17.4k Golang : Check if IP address is version 4 or 6
+12.1k Golang : Pagination with go-paginator configuration example