-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set a nil date to NULL rather than 0000-00-00 #346
Conversation
@julienschmidt Do you have any considerations about this change? |
👍 |
I'm not entirely convinced. There is not really a "nil date" since time.Time is a number. Therefore it's default value is 0, not nil. There is also a semantic difference between On the other hand, @arnehormann @xaprb |
|
But *time.Time is not a valid database/sql Value. Is it converted to an uninitialized |
Nor
Specifically, my internal code works with |
database/sql converts all these values to the values the driver must be able to handle: We don't know which of the values the user originally passed. So we don't know when you passed |
I really dislike all the type handling magic. Especially when it comes in form of dsn parameters - and this could easily become yet another one. |
I especially hate the magic the database/sql package does |
Me too... I hate that it's needed at all. |
@julienschmidt This issue has been open for a while now, perhaps we can do something about it? |
Maybe fix the tests so it can be merged? |
I just spent 1-2 hours (longer than the time spent on the change itself) on trying to comprehend how the TestDateTime() stuff works. During that time I wasn't able to come up with a way that would fail without my change, and pass with my change. The TestDateTime tries to cover (imho) too many things at once, and it's taking me too long to come up with something sensible. If you have any ideas how to test this sensibly I'd be happy to hear it. If not, I'll close this pull request. |
I am running into this exact issue with strict set on in mysql 5.7.10 using gorm with time.Time fields in structs. It seems very reasonable to me that a time.Time{} (uninitialized/zero value) for a NULLable database datetime column should be inserted as NULL (not '0000-00-00'). Is there any reason not to accept this PR? With strict=true the existing code simply doesn't work in 5.7 (zero dates are not allowed). |
0000-00-00 is not accepted by default from MySQL 5.7. |
This PR needs an update then |
+1 for using @Freeaqingme thoughts on updating? |
@tab1293 Sorry, right now I've got different priorities. Though, please feel free to check out my branch and do whatever needs to be done. |
I tried but it's difficult than I thought. Currently, this driver support passing time.Time for TIME column. And |
This should be fixed soon, but the chances that we do something wrong that breaks things is high. |
Related: #783 |
Any update on this guys ? |
There are no valid solution. |
You can not assign null value to time.Time variable |
You can pass nil to DB.Exec() and DB.Query(). |
Now I think we should drop support passing At some point, |
use |
What new about this problem? |
No plan to convert Zero Time to NULL. You can use NullTime instead. I suggest closing this issue because the merit of this change is not large enough When we want to set NULL, we can use nil instead of zero Time. |
There is a chance to do this, but there are no timeline for it because of the upstream issue.
It may take more than one year. Don't wait this issue is fixed. Use NullTime or nil instead. |
It work for me to set SQL timestamp to null |
I've got a row defined: "date_disconnect" datetime DEFAULT NULL
I'm also running mysql in strictmode:
I set these modes because if I want to indicate the absence of a data, I like to use NULL, rather than set an "arbitrary" date 2015 years ago. In line with ANSI SQL.
The DSN used: root:@tcp(localhost:3306)/cluegetter?sql_notes=false&strict=true
Please let me know what you think of this PR. I'll get some tests if you agree on the chosen approach.