#29139: Aggregate functions failing when using Postgres JSON field KeyTransform
-------------------------------------+-------------------------------------
               Reporter:  trik       |          Owner:  (none)
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  2.0
  contrib.postgres                   |       Keywords:  json keytransform
               Severity:  Normal     |  unhashable
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 When annotating by a nested KeyTransform applied to a JSON field,
 aggregate functions fail:

 {{{#!python
 JSONModel.objects.annotate(history=KeyTransform('-1', 'field'))\
     .annotate(last_state=KeyTransform('state', 'history'))\
     .filter(last_state__gte=5).count()

 JSONModel.objects.annotate(history=KeyTransform('-1', 'field'))\
     .annotate(last_state=KeyTransform('state', 'history'))\
     .filter(last_state__isnull=False).aggregate(Sum('last_state'))
 }}}

 {{{
 Traceback (most recent call last):
   File
 "/Users/trik/Projects/3d_party/django/tests/postgres_tests/test_json.py",
 line 305, in test_keytransform
     .filter(last_state__gte=5).count(),
   File "/Users/trik/Projects/3d_party/django/django/db/models/query.py",
 line 384, in count
     return self.query.get_count(using=self.db)
   File
 "/Users/trik/Projects/3d_party/django/django/db/models/sql/query.py", line
 494, in get_count
     number = obj.get_aggregation(using, ['__count'])['__count']
   File
 "/Users/trik/Projects/3d_party/django/django/db/models/sql/query.py", line
 462, in get_aggregation
     outer_query.add_subquery(inner_query, using)
   File
 "/Users/trik/Projects/3d_party/django/django/db/models/sql/subqueries.py",
 line 193, in add_subquery
     self.subquery, self.sub_params =
 query.get_compiler(using).as_sql(with_col_aliases=True)
   File
 "/Users/trik/Projects/3d_party/django/django/db/models/sql/compiler.py",
 line 443, in as_sql
     extra_select, order_by, group_by = self.pre_sql_setup()
   File
 "/Users/trik/Projects/3d_party/django/django/db/models/sql/compiler.py",
 line 55, in pre_sql_setup
     group_by = self.get_group_by(self.select + extra_select, order_by)
   File
 "/Users/trik/Projects/3d_party/django/django/db/models/sql/compiler.py",
 line 130, in get_group_by
     if (sql, tuple(params)) not in seen:
 TypeError: unhashable type: 'list'
 }}}

 
[[https://github.com/gnucoop/django/commit/34769e05be720792e8afc2aebdac0230a77e04c0|Failing
 test case]]

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.2d98542573e0eee81d7c31f69d3b8384%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to