Skip to content

Detecting duplicate queries breaks for non-hashable query params #1091

Closed
@tilboerner

Description

@tilboerner

Since commit 844353e, SQL query params are used to detect duplicate queries. A duplicate_key is created and then used as a key to a dict:

https://github.com/jazzband/django-debug-toolbar/blob/05ffccfb68fac64eeba9894707f9b6771e462648/debug_toolbar/panels/sql/panel.py#L151-L153

https://github.com/jazzband/django-debug-toolbar/blob/844353e4cb9855d76e4fa8b0f48de140d2588204/debug_toolbar/panels/sql/panel.py#L177

https://github.com/jazzband/django-debug-toolbar/blob/844353e4cb9855d76e4fa8b0f48de140d2588204/debug_toolbar/panels/sql/panel.py#L248


This fails when raw_params contains non-hashable items like lists. I saw it happen for the following query using a Postgres ArrayField, but it should also be triggered by a simple .filter(pk__in=[1, 2, 3]):

cls.objects.filter(
            Q(to_emails__contains=[customer.email]) |
            Q(cc_emails__contains=[customer.email]) |
            Q(bcc_emails__contains=[customer.email])
        )
Traceback (most recent call last):
  File "/.../lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/.../lib/python3.6/site-packages/django/utils/deprecation.py", line 97, in __call__
    response = self.process_response(request, response)
  File "/.../lib/python3.6/site-packages/debug_toolbar/middleware.py", line 126, in process_response
    panel.generate_stats(request, response)
  File "/.../lib/python3.6/site-packages/debug_toolbar/panels/sql/panel.py", line 178, in generate_stats
    query_duplicates[alias][duplicate_key(query)] += 1
TypeError: unhashable type: 'list'

(Edit: passing lists to __in works fine.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions