[Django] #29767: Django ORM: Cast between datetime and date

11 views
Skip to first unread message

Django

unread,
Sep 18, 2018, 4:46:16 AM9/18/18
to django-...@googlegroups.com
#29767: Django ORM: Cast between datetime and date
-----------------------------------------+------------------------
Reporter: Rémy Hubscher | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
While using with Annotations and Cast I realized that the casting between
datetime and date with SQLite3 was kinda broken.


{{{
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.

Django

unread,
Sep 18, 2018, 4:48:07 AM9/18/18
to django-...@googlegroups.com
#29767: Django ORM: Cast between datetime and date
-------------------------------------+-------------------------------------

Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:1>

Django

unread,
Sep 18, 2018, 5:18:32 AM9/18/18
to django-...@googlegroups.com
#29767: Django ORM: Cast between datetime and date
-------------------------------------+-------------------------------------

Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* 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>

Django

unread,
Sep 18, 2018, 7:45:54 AM9/18/18
to django-...@googlegroups.com
#29767: Django ORM: Cast between datetime and date
-------------------------------------+-------------------------------------

Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 18, 2018, 8:35:01 AM9/18/18
to django-...@googlegroups.com
#29767: Django ORM: Cast between datetime and date
-------------------------------------+-------------------------------------

Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* 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>

Django

unread,
Sep 26, 2018, 2:54:46 PM9/26/18
to django-...@googlegroups.com
#29767: Django ORM: Cast between datetime and date
-------------------------------------+-------------------------------------

Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* 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>

Django

unread,
Sep 26, 2018, 3:22:02 PM9/26/18
to django-...@googlegroups.com
#29767: Failed to cast between datetime and date on SQLite
-------------------------------------+-------------------------------------

Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:6>

Django

unread,
Sep 28, 2018, 11:24:18 AM9/28/18
to django-...@googlegroups.com
#29767: Failed to cast between datetime and date on SQLite
-------------------------------------+-------------------------------------
Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29767#comment:7>

Django

unread,
Oct 1, 2018, 7:29:40 AM10/1/18
to django-...@googlegroups.com
#29767: Failed to cast between datetime and date on SQLite
-------------------------------------+-------------------------------------
Reporter: Rémy Hubscher | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: sqlite | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz <claude@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages