[Django] #24176: Incorrect SQL text when searching for datetime values with milliseconds

17 views
Skip to first unread message

Django

unread,
Jan 18, 2015, 1:58:24 PM1/18/15
to django-...@googlegroups.com
#24176: Incorrect SQL text when searching for datetime values with milliseconds
----------------------------------------------+--------------------
Reporter: me21 | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I have SQLite database where certain column in table is created as
TIMESTAMP. The database is created not by Django, but by SymmetricDS, a DB
replication software. The database is replicated from Microsoft SQL Server
2005, where it has datetime type. In my Django application, the model has
DateTimeField for that column.
{{{#!python
class Calendar(models.Model):
day_id = models.IntegerField(primary_key=True)
day_date = models.DateTimeField()
}}}
The table contains a row with day_date equal to '2015-01-18 00:00:00.000'.

If I try to search for that row with
{{{#!python
day = Calendar.objects.filter(day_date=date(2015,1,18))
}}}
I get SQL with the following WHERE clause:
{{{#!sql
WHERE day_date = '2015-01-18 00:00:00'
}}}
, i.e. it doesn't contain milliseconds part. As a consequence, the row is
not found. According to http://www.sqlite.org/datatype3.html, the text
representation of datetime values in SQLite should have milliseconds part.

In MS SQL it works fine (I use django-pyodbc-azure driver).

The workaround is:
{{{#!python
dt = datetime(2015,1,18) # datetime, not date
day = Calendar.objects.filter(day_date__lt=day+timedelta(seconds=1),
day_date__gt=day-timedelta(seconds=1))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24176>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 18, 2015, 1:59:12 PM1/18/15
to django-...@googlegroups.com
#24176: Incorrect SQL text when searching in SQLite for datetime values with
milliseconds
-------------------------------------+-------------------------------------

Reporter: me21 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(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 me21):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
Jan 18, 2015, 4:36:49 PM1/18/15
to django-...@googlegroups.com
#24176: Incorrect SQL text when searching in SQLite for datetime values with
milliseconds
-------------------------------------+-------------------------------------

Reporter: me21 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(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
-------------------------------------+-------------------------------------

Comment (by shaib):

Hi, thanks for your report.

Can you please try these two variations:

a) `filter(day_date=datetime(2015,1,18))`

b) The whole thing on a newer Django -- 1.6 now gets important (that is,
security or data-loss) fixes only, and I don't think this qualifies.

Thanks again.

--
Ticket URL: <https://code.djangoproject.com/ticket/24176#comment:2>

Django

unread,
Jan 20, 2015, 8:39:23 AM1/20/15
to django-...@googlegroups.com
#24176: Incorrect SQL text when searching in SQLite for datetime values with
milliseconds
-------------------------------------+-------------------------------------

Reporter: me21 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7

(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 me21):

* version: 1.6 => 1.7


Comment:

The bug still exists in Django 1.7.3, and
{{{filter(day_date=datetime(2015,1,18))}}} doesn't change anything.

--
Ticket URL: <https://code.djangoproject.com/ticket/24176#comment:3>

Django

unread,
Feb 3, 2015, 1:37:27 PM2/3/15
to django-...@googlegroups.com
#24176: Incorrect SQL text when searching in SQLite for datetime values with
milliseconds
-------------------------------------+-------------------------------------

Reporter: me21 | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24176#comment:4>

Django

unread,
Sep 27, 2018, 12:13:15 PM9/27/18
to django-...@googlegroups.com
#24176: Incorrect SQL text when searching in SQLite for datetime values with
milliseconds
-------------------------------------+-------------------------------------
Reporter: Alexandr Zarubkin | Owner: nobody
Type: Bug | Status: closed

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

Keywords: | 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):

* status: new => closed
* resolution: => fixed


Comment:

Since Django 1.9, the query should force to date with:
`Calendar.objects.filter(day_date__date=date(2015,1,18))`

--
Ticket URL: <https://code.djangoproject.com/ticket/24176#comment:5>

Reply all
Reply to author
Forward
0 new messages