﻿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
30855	"JSON has_key causes 'TypeError: can only concatenate tuple (not ""list"") to tuple' in Django 2.2.6"	Shaheed Haque	nobody	"In Django 2.2.6, but not in Django 2.2.5, this query causes the exception shown:




{{{
>>> models.PayRun.objects.filter(snapshot__employee__has_key='6')
Traceback (most recent call last):
  File ""/usr/lib/python3.6/code.py"", line 91, in runcode
    exec(code, self.locals)
  File ""<console>"", line 1, in <module>
  File ""/usr/local/lib/python3.6/dist-packages/django/db/models/query.py"", line 250, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File ""/usr/local/lib/python3.6/dist-packages/django/db/models/query.py"", line 274, in __iter__
    self._fetch_all()
  File ""/usr/local/lib/python3.6/dist-packages/django/db/models/query.py"", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/usr/local/lib/python3.6/dist-packages/viewflow/managers.py"", line 87, in __iter__
    for process in base_iterator:
  File ""/usr/local/lib/python3.6/dist-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 ""/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py"", line 1087, in execute_sql
    sql, params = self.as_sql()
  File ""/usr/local/lib/python3.6/dist-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 ""/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py"", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File ""/usr/local/lib/python3.6/dist-packages/django/db/models/sql/where.py"", line 81, in as_sql
    sql, params = compiler.compile(child)
  File ""/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py"", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File ""/usr/local/lib/python3.6/dist-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 model field in question looks like this:


{{{
    snapshot = JSONField(encoder=DjangoJSONEncoder, default=dict, blank=True)
}}}

The code and variables [https://github.com/django/django/blob/dafdfd6a60638c4edcca7c4e65d11c0af654d759/django/contrib/postgres/lookups.py#L11 at the point of failure] look like this:


{{{
(Pdb) list
  6  
  7     class PostgresSimpleLookup(Lookup):
  8         def as_sql(self, qn, connection):
  9             lhs, lhs_params = self.process_lhs(qn, connection)
 10             rhs, rhs_params = self.process_rhs(qn, connection)
 11 B->         params = lhs_params + rhs_params
 12             return '%s %s %s' % (lhs, self.operator, rhs), params
 13  
 14  
 15     class DataContains(PostgresSimpleLookup):
 16         lookup_name = 'contains'
(Pdb) lhs
'(""paiyroll_payrun"".""snapshot"" -> %s)'
(Pdb) lhs_params
('employee',)
(Pdb) rhs
'%s'
(Pdb) rhs_params
['6']
}}}

Thus, process_lhs() and process_rhs() are producing results which cannot be combined.

"	Bug	closed	Database layer (models, ORM)	1.11	Normal	duplicate			Unreviewed	0	0	0	0	0	0
