{{{
Cast("datetime_field", DateField())
}}}
is transformed into
{{{
CAST("datetime_field" AS TEXT)
}}}
rather than for instance
{{{
substr(CAST(datetime_field AS TEXT),0,11)
}}}
I was able to use extra to fix that and run the query. However I would
like to fix this.
This is especially useful for my usecase where I want to GROUP BY the
annotated value.
I wrote a test for it https://github.com/django/django/pull/10387/files
--
Ticket URL: <https://code.djangoproject.com/ticket/29767>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:1>
* keywords: => sqlite
* stage: Unreviewed => Accepted
Comment:
After a slight modification of the provided tests, I can confirm that they
pass with PostgreSQL and MySQL, but not on SQLite.
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:2>
Comment (by Claude Paroz):
Note that we should also test/handle casts to time and datetime.
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:3>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
Comment:
In [https://github.com/django/django/pull/10402 this PR], the test
`db_functions.comparison.test_cast.CastTests.test_cast_from_python_to_datetime`
fails because casting a Python datetime to a SQLite3 datetime is rounding
the fractional seconds to the first 3 digits.
Now I'm not sure if we should fix this or not, and if we should, if this
can be fixed on SQLite. Casting a datetime to a datetime seems a bit
strange. Generally, when you cast to datetime, you have a type with less
precision (typically a date).
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:4>
* needs_better_patch: 1 => 0
Comment:
I updated the PR by adding a database feature for the precision loss of
SQLite when casting to datetime.
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:5>
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:6>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:7>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"fc3a463048e52969f208a3e7821138d92b485aa6" fc3a4630]:
{{{
#!CommitTicketReference repository=""
revision="fc3a463048e52969f208a3e7821138d92b485aa6"
Fixed #29767 -- Made date-related casts work on SQLite
Thanks Rémy Hubscher for the report and Tim Graham and Simon Charette for
the reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:8>