I am using MYSQL as a database and gorp as the ORM library.
It's getting an error on the Updated_At column because it is null. This is how I have it written in my model: Updated_At time.Time `db: "updated_at".
The work around I'm thinking about is initializing both Updated_At and Deleted_at to 0001-01-01 00:00:00 +0000 UTC. Is there a better work around?
The error btw:
sql: Scan error on column index 7: unsupported Scan, storing driver.Value type <nil> into type *time.Time
评论:
tgulacsi:
chaseroyaleee:It says that use *time.Time.
tgulacsi:Value type <nil> into type *time.Time
Ohhhh. It didn't sound that explicit to me sorry haha. It's working now!
chaseroyaleee:Yup, in reál, it is a little bit more involved: driver.Value is an empty interface, it is "unpacked" into the pointer given in Scan; nil needs a pointer to a pointer, thus &(*time.Time).
media_guru:Makes sense makes sense. Having this problem right now with posting the time. It's automatically making it 000-01-01, the 0 for time. Thinking about just changing the model from time.Time to an int instead. I'll make time to UNIX based. What do you think?
interactiv_:The mysql driver has a NullTime struct you can use. It has two fields: Valid (bool) and Time (time.Time)
Time will always be set and use the default time.Time value if the column was null. If it was null, Valid will be false
Be aware that if you use the same struct for json marshalling, that NullTime struct is gonna get marshaled as is.
chaseroyaleee:You'll get a lot of these everywhere there are potential null values and it sucks. If Go sql package was more like JDBC it would have been more manageable for any library that extends sql. So you either have to change your schema to eliminate null values, use pointers of int,string,... in your structs or use funky types provided by drivers. This is not a good API no matter what people say.
nathj07:Yeah just ended up making them int64s and using Unix time because its a hassle haha
That's the empty value for time.Time. You can check it with the convenience function time.IsZero(). I've not used mysql with Go but in PostgreSQL the lib/pq library has a NullTime type that works great on these situations.