Gorm: Columns with SQL "DATE" type do not match Golang `time.Time` type.

Created on 29 May 2016  路  6Comments  路  Source: go-gorm/gorm

So, there doesn't seem to be a Golang column type for the MySQL DATE() column type. INSERT'ing a time.Time value into a DATE() field correctly INSERTs the date, but when SELECT'ing rows it does not match without casting the value as DATE() first.

In other words, MySQL can intelligently convert a DATETIME() created from time.Time to DATE() when INSERT'ing the data, but when SELECT'ing, it won't match the time.Time generated DATETIME() value. When using struct matching in db.Where(), there's no way to cast the DATETIME() to a DATE().

Most helpful comment

When opening database are you setting parseTime=true https://github.com/go-sql-driver/mysql#timetime-support

All 6 comments

Temporary workaround:

db.Where("day=DATE(?)", time.Now()).Where(&structToMatch).First(&structToFetchInto);

... Would be nice if it'd recognize that it's a DATE() column and cast the DATETIME(). Also, this might be an issue with the SQL driver not GORM, not sure.

Maybe you can use MySQL 'CONVERT()' to get the date/datetime you are expecting. Check this out:
http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html

think that if were a driver/gorm problem you wont be able to fetch date/datetime types, so it might be only a matter of cast/parse data into a correct type.

Hope it helps!

How is that going to help? It's a matter of SELECT-ing, and GORM doesn't detect that the column (in the model) is a DATE not a DATETIME.

When opening database are you setting parseTime=true https://github.com/go-sql-driver/mysql#timetime-support

Yes, of course

sounds like a driver issue, maybe you could open an issue there.

Was this page helpful?
0 / 5 - 0 ratings