Opened 5 years ago

Closed 5 years ago

#31105 closed Bug (duplicate)

Search within an array in JSON Field

Reported by: Tharshan Muthulingam Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Searching for a value inside an array contained in a JSONField structure, was possible with the Django 2.0.8

However, it results in an exception when using Django 2.2

  File "/Users/viperfx/Projects/sample/sample/utils.py", line 61, in update_user_crontab_schedule
    member_reports = Report.objects.filter(team=team, data__users__contains=member_id)
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1087, in execute_sql
    sql, params = self.as_sql()
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 489, in as_sql
    where, w_params = self.compile(self.where) if self.where is not None else ("", [])
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/sql/where.py", line 81, in as_sql
    sql, params = compiler.compile(child)
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/contrib/postgres/lookups.py", line 11, in as_sql
    params = lhs_params + rhs_params
TypeError: can only concatenate tuple (not "list") to tuple

The query being executed is

member_reports = Report.objects.filter(team=team, data__users__contains=member_id)

The sample of the JSONField that is being queried:

{
    "users": ["1n7HPbf7yxBqECykJ0mk3uCubFRAI1ee8IFk", "QYNz3iXyMGhi0TmQn2L0Ke4Y9xk6xkEGu9lM3wPVscmqODA7yw"]
}

Change History (1)

comment:1 by Simon Charette, 5 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #30826 which is fixed in Django 2.2.7+.

By looking at the last line of your traceback

...
File "/Users/viperfx/.pyenv/versions/env/lib/python3.7/site-packages/django/contrib/postgres/lookups.py", line 11, in as_sql
    params = lhs_params + rhs_params

It's clear that it doesn't contain the tuple wrapping included in the patch.

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