#29684: PostgreSQL causes "TypeError: unhashable type: 'list'" when using 
aggregate
function
-------------------------------------+-------------------------------------
               Reporter:  ptrstn     |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:             |        Version:  2.0
  Uncategorized                      |       Keywords:  PostgreSQL,
               Severity:  Normal     |  aggregate, Count, tuple, array,
           Triage Stage:             |  annotate, unhashable type, list
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I get following error message:


 {{{
 self = <When: WHEN <Q: (AND: ('attribute_a__in', [1]))> THEN Value(yes)>

     def __hash__(self):
         path, args, kwargs = self.deconstruct()
         h = hash(path) ^ hash(args)
         for kwarg in kwargs.items():
 >           h ^= hash(kwarg)
 E           TypeError: unhashable type: 'list'

 venv\lib\site-packages\django\db\models\expressions.py:386: TypeError
 }}}

 running this test code:

 {{{
 def test_mymodel(self):
   my_models = MyModel.objects \
                       .annotate(my_annotation=Case(
                         When(attribute_a__in=[1, 2], then=Value('yes')),
                         default=Value('no'),
                         output_field=CharField())
                       ) \
                      .annotate(count_a=Count("my_annotation"))
   print(my_models)
 }}}

 For this model:
 {{{
 class MyModel(models.Model):
     attribute_a = models.IntegerField()
 }}}


 The complete test code can be checked at https://github.com/random-code-
 chunks/django_postgres_aggregate_problem

 As you can see in travis-ci, this bug only appears in version 2.0 of
 django and is fixed in 2.1 and 1.11 (https://travis-ci.com/random-code-
 chunks/django_postgres_aggregate_problem)

 This error does not appear when using SQLite.

 The above test case can be fixed using a tuple instead of a list

 {{{
 When(attribute_a__in=(1, 2), then=Value('yes')),
 }}}


 Somehow related:

 https://code.djangoproject.com/ticket/28762
 https://code.djangoproject.com/ticket/29139

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29684>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.afb5783305438bb0647db23074bdb185%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to