#24925 closed Bug (fixed)
Coalesce converts datetime to string on MySQL
Reported by: | Ian Foote | Owned by: | Ian Foote |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Passing a datetime
to Coalesce
leads to a string annotation
instead of a datetime
.
Change History (9)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
Is this an issue with MySQL or the implementation of Coalesce within Django? That is, does MySQL convert datetimes to strings in coalesce or does Django?
comment:3 by , 9 years ago
I've replicated the issue using mysqlclient 1.3.6
directly, so Django isn't causing this.
comment:4 by , 9 years ago
I think we should at least document that passing values directly to Coalesce
on MySQL can cause problems without an explicit cast
.
comment:5 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 9 years ago
The first comment is not clear to me: It may refer to
cast (coalesce (field, now) as datetime)
or to
coalesce (field, cast (now as datetime))
The latter may be done completely automatically by Django, the former may also be done automatically if an output field is provided.
Also note https://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html -- before 5.6.4 MySql understood date literals as strings, not sure how that applies to embedded variables; please try to re-test this with a later-than-5.6.4 version.
comment:7 by , 9 years ago
I meant the latter example. The cast does not appear to being automatically applied by Django. I've just tested using MySQL 5.6.19 and the problem still appears.
A workaround for this is to
cast
the python datetime value to a MySQL datetime usingRawSQL
:RawSQL("cast(%s as datetime)", (datetime,))
.