[Django] #31058: Ordering by the result of RawSQL with F expression and distinct

12 views
Skip to first unread message

Django

unread,
Dec 3, 2019, 5:50:18 PM12/3/19
to django-...@googlegroups.com
#31058: Ordering by the result of RawSQL with F expression and distinct
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) | Keywords: RawSQL, ordering,
Severity: Normal | expression
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi,
I run into an exception when combining `RawSQL`, `distinct` and `order_by`
with an expression:
{{{
ids = [10]
params = (tuple(ids), )
queryset = Item.objects.annotate(
score=RawSQL("""
SELECT SUM(id) AS score
FROM app_item
WHERE id IN %s""",
params=params,
output_field=FloatField()),
).order_by(F("score").desc(nulls_last=True)).distinct()
print(queryset.query)
}}}
where `Item` is any model you want, as I'm only using its `id`.

The exact exception is: `TypeError: not all arguments converted during
string formatting`

The exception seems due to the `sql_with_params` method of
`django.db.models.sql.Query` duplicating the `params` one time too many :
a `print(params)` at line 257 in django/db/models/sql/query.py outputs
`(('10',), ('10',), ('10',))`, while the query has only two placeholders.

I'm running Django 2.2.6

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

Django

unread,
Dec 3, 2019, 11:18:18 PM12/3/19
to django-...@googlegroups.com
#31058: Ordering by the result of RawSQL with F expression and distinct
-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: RawSQL, ordering, | Triage Stage:
expression | Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Simon Charette):

Could you try reproducing against the recently released 3.0 version, I
vaguely remember a similar issue that was addressed in the past weeks.

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

Django

unread,
Dec 4, 2019, 2:35:15 AM12/4/19
to django-...@googlegroups.com
#31058: Ordering by the result of RawSQL with F expression and distinct.

-------------------------------------+-------------------------------------
Reporter: wilhelmhb | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: RawSQL, ordering, | Triage Stage:
expression | 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: => duplicate


Comment:

Executing this query in Django 2.2 fails with
{{{
django.db.utils.ProgrammingError: syntax error at or near "WHERE"
LINE 4: ... WHERE id IN (10)) AS "score", WHERE id I...
}}}
but it was fixed in 567b9928a3ad37e95b9ae17ec41342daa6968739.

I cannot reproduce `TypeError` when calling `print(queryset.query)`. I
think we can mark this as a duplicate of #29692.

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

Reply all
Reply to author
Forward
0 new messages