Opened 8 years ago

Last modified 12 months ago

#25937 new Bug

Failure when using expressions.DateTime on NULL values and aggregating — at Initial Version

Reported by: Thomas Recouvreux Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords: aggregate timezone
Cc: cherie@…, Sarah Boyce Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Running query like qs.annotate(time=DateTime('removed'), 'month', timezone.UTC()).values('time').annotate(Count('pk')) fails with <repr(<gill.contrib.sales.models.TransactionRowQuerySet at 0x10cc99320>) failed: ValueError: Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?>. I noted the failure on MySQL and PostgreSQL, Django 1.8.X and 1.9.X. I did not try on other db backends or other Django versions.

To reproduce the behaviour:

  1. create a model:
    class Potato(models.Model):
        ..
        removed = models.DateTimeField(null=True, blank=True)
    
  1. Insert some of them
    Potato.objects.create(removed=timezone.now())
    Potato.objects.create(removed=None)
    ..
    
  1. Fire the request
    # Failure
    Potato.annotate(time=DateTime('removed'), 'month', timezone.UTC()).values('time').annotate(c=Count('pk'))
    
    # Success
    Potato.filter(removed__isnull=False).annotate(time=DateTime('removed'), 'month', timezone.UTC()).values('time').annotate(c=Count('pk'))
    

Note: I printed the raw sql generated by Django and fired it directly in a db shell without any error, so the problem seems to come from the code Django uses to convert the result from the backend to python code, especially the line with NULL coming from the aggregation:

+---------------------+-----------+
| time                | pk__count |
+---------------------+-----------+
| NULL                |        17 |
| 2015-01-01 00:00:00 |         7 |
..

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top