Opened 22 months ago

Closed 22 months ago

Last modified 22 months ago

#29108 closed Bug (fixed)

QuerySet.distinct().order_by()[slice].count() crashes with "TypeError: can only concatenate tuple (not "list") to tuple"

Reported by: Stephen Brooks Owned by: Simon Charette
Component: Database layer (models, ORM) Version: 2.0
Severity: Release blocker Keywords: TypeError list tuple
Cc: felixxm Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Seen in django version 2.0.2
A model query triggers line 1448 in the file django/db/models/sql/compiler.py to raise a TypeError:

params = params + self.query.sub_params

TypeError exception: can only concatenate tuple (not "list") to tuple

This line is in the as_sql method of the SQLAggregateCompiler class.

How to reproduce:
Issue a query on a model which is related to another model, and include distinct, order_by (on a string field in the related model), a slice and a call to count().

e.g.

Post.objects.distinct().order_by('author__last_name')[:20].count()

The presence of distinct() above, you could argue, is not needed, however, the above represents a simplification of the more complex query with which I originally saw the problem, and there the distinct() followed a more complex cross-table filter.

Change History (7)

comment:1 Changed 22 months ago by felixxm

Cc: felixxm added

comment:2 Changed 22 months ago by felixxm

Can you give more details about a Post and Author models? I couldn't reproduce this issue.

comment:3 in reply to:  2 Changed 22 months ago by Stephen Brooks

Replying to felixxm:

Can you give more details about a Post and Author models? I couldn't reproduce this issue.

They are just sample model names. The important thing is that there is a foreign key relationship from Post to Author so that the query can do an order_by on a char field in the related model.

When trying to reproduce it, don't leave out any of: distinct, order_by(a char field in the related model), slice, and count.

comment:4 Changed 22 months ago by Tim Graham

Component: UncategorizedDatabase layer (models, ORM)
Severity: NormalRelease blocker
Summary: TypeError: can only concatenate tuple (not "list") to tuple raised by compiler.pyQuerySet.distinct().order_by()[slice].count() crashes with "TypeError: can only concatenate tuple (not "list") to tuple"
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

I reproduced with Book.objects.distinct().order_by('publisher__name')[:20].count() for tests/aggregation (not the right place commit the test). Bisected to 4acae21846f6212aa992763e587c7e201828d7b0.

comment:5 Changed 22 months ago by Simon Charette

Has patch: set
Owner: changed from nobody to Simon Charette
Status: newassigned

comment:6 Changed 22 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In d61fe246:

Fixed #29108 -- Fixed crash in aggregation of distinct+ordered+sliced querysets.

Regression in 4acae21846f6212aa992763e587c7e201828d7b0.
Thanks Stephen Brooks for the report.

comment:7 Changed 22 months ago by Tim Graham <timograham@…>

In 8d03356:

[2.0.x] Fixed #29108 -- Fixed crash in aggregation of distinct+ordered+sliced querysets.

Regression in 4acae21846f6212aa992763e587c7e201828d7b0.
Thanks Stephen Brooks for the report.

Backport of d61fe246015aa4fdc6dcb837ffb1442fa71ae586 from master

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