Opened 6 years ago

Closed 6 years ago

#29684 closed Bug (wontfix)

PostgreSQL causes "TypeError: unhashable type: 'list'" when using aggregate function

Reported by: Peter Stein Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords: PostgreSQL, aggregate, Count, tuple, array, annotate, unhashable type, list
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (1)

comment:1 by Tim Graham, 6 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: wontfix
Status: newclosed

Per our supported versions policy, Django 2.0 is only receiving fixes for security and data loss issues.

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