[Django] #30788: Fix concatenation of tuple to list (or vice versa)

28 views
Skip to first unread message

Django

unread,
Sep 19, 2019, 2:52:21 PM9/19/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry | Owner: nobody
Mugtasimov |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords: postgresql
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Sometimes you may get
{{{
TypeError: can only concatenate tuple (not "list") to tuple
}}}

As you see it happens because lhs_params is tuple and rhs_params is list.
{{{
def as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
> params = lhs_params + rhs_params
E TypeError: can only concatenate tuple (not "list") to tuple

connection = <django.db.backends.postgresql.base.DatabaseWrapper object at
0x7f4845880b38>
lhs = '(SELECT ARRAY_AGG(U0."experience_id" ) AS
"experience_levels" FROM "jobs_jobtitleexperiencetrough" U0 WHERE
U0."title_id" = ANY(("jobs_job"."titles")))'
lhs_params = ()
qn = <django.db.models.sql.compiler.SQLCompiler object at
0x7f483d658860>
rhs = '%s'
rhs_params = [(21,)]
self = <django.contrib.postgres.fields.array.ArrayContains object at
0x7f483d67a0f0>

}}}

This is because of implementation of PostgresSimpleLookup:

{{{
class PostgresSimpleLookup(Lookup):
def as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
params = lhs_params + rhs_params
return '%s %s %s' % (lhs, self.operator, rhs), params
}}}

I use this workaround:

{{{
class FixedArrayContains(DataContains):
def super_as_sql(self, qn, connection):
lhs, lhs_params = self.process_lhs(qn, connection)
rhs, rhs_params = self.process_rhs(qn, connection)
params = list(lhs_params) + list(rhs_params)
return '%s %s %s' % (lhs, self.operator, rhs), params

def as_sql(self, qn, connection):
sql, params = self.super_as_sql(qn, connection)
sql = '%s::%s' % (sql, self.lhs.output_field.db_type(connection))
return sql, params

}}}

Please, provide a solution

--
Ticket URL: <https://code.djangoproject.com/ticket/30788>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 20, 2019, 2:32:17 AM9/20/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry Mugtasimov | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: needsinfo
Keywords: postgresql | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 2.2 => master
* resolution: => needsinfo


Comment:

Thanks for the report, however I cannot reproduce this issue on
bae05bcf68710cb6dafa51325c3ec83ddda83c39. Can you provide models and a
queryset?

--
Ticket URL: <https://code.djangoproject.com/ticket/30788#comment:1>

Django

unread,
Oct 1, 2019, 10:59:56 AM10/1/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry Mugtasimov | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: postgresql | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by mcosta74):

* status: closed => new
* resolution: needsinfo =>


Comment:

I can confirm this is a BUG introduced in v2.2.6 most likely in
https://code.djangoproject.com/ticket/30769

In my case was about a QuerySet with a filter on a JSONField
Here my model

{{{
class CustomFieldDefinition(models.Model):
"""
Custom field
"""

....

metadata = JSONField(
verbose_name="Additional information relative to the field.",
default=dict,
)
}}}

and this is my queryset

{{{
qs = CustomFieldDefinition.objects.filter(pk__in=values) \
.filter(metadata__LDAP__has_key='attributename') \
.values_list('metadata', flat=True)
}}}

as soon as I run

{{{
qs.all()
}}}

I get the same error

in v2.2.5 the code works

--
Ticket URL: <https://code.djangoproject.com/ticket/30788#comment:2>

Django

unread,
Oct 1, 2019, 11:06:27 AM10/1/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry Mugtasimov | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: postgresql | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by mcosta74):

* cc: mcosta74 (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/30788#comment:3>

Django

unread,
Oct 1, 2019, 11:44:08 AM10/1/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry Mugtasimov | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: postgresql | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* status: new => closed

* resolution: => fixed


Comment:

Please provide the full traceback of the exception you are encountering.
It's almost impossible to trace down without it.

--
Ticket URL: <https://code.djangoproject.com/ticket/30788#comment:4>

Django

unread,
Oct 1, 2019, 11:45:59 AM10/1/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry Mugtasimov | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: postgresql | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I suspect
https://github.com/django/django/blob/67e7dffe9543aff259f63c8f12d15642fe7be100/django/contrib/postgres/fields/jsonb.py#L110
is the culprit here.

--
Ticket URL: <https://code.djangoproject.com/ticket/30788#comment:5>

Django

unread,
Oct 1, 2019, 3:26:14 PM10/1/19
to django-...@googlegroups.com
#30788: Fix concatenation of tuple to list (or vice versa)
-------------------------------------+-------------------------------------
Reporter: Dmitry Mugtasimov | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: postgresql | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* resolution: fixed => duplicate


Comment:

Looks like a duplicate of #30826. Letting this one closed because the
other has the regression test attached.
Thanks for reporting!

--
Ticket URL: <https://code.djangoproject.com/ticket/30788#comment:6>

Reply all
Reply to author
Forward
0 new messages