﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29684	"PostgreSQL causes ""TypeError: unhashable type: 'list'"" when using aggregate function"	Peter Stein	nobody	"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
"	Bug	closed	Database layer (models, ORM)	2.0	Normal	wontfix	PostgreSQL, aggregate, Count, tuple, array, annotate, unhashable type, list		Unreviewed	0	0	0	0	0	0
