[Django] #28484: QuerySet.extra keyword

5 views
Skip to first unread message

Django

unread,
Aug 9, 2017, 7:21:17 AM8/9/17
to django-...@googlegroups.com
#28484: QuerySet.extra keyword
--------------------------------------------+------------------------
Reporter: Antonio Carrasco | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
Related to the #extra deprecation
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#extra
Given this example:

queryset = Model.objects.all()
ordered_ids = "1,3,5,7"
queryset.annotate(custom=RawSQL(f"SELECT FIELD(id, %s)",
(ordered_ids,))).order_by("custom")
queryset.extra(select={"custom": f"FIELD(id, {ordered_ids})"},
order_by=[custom]).distinct()


The result of both queries is different. But executing the
.query.__str__() query in the Mysql native console, the result is the
same.
The result with #extra is ok (the same the native mysql console returns)
but the result with annotate is wrong.

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

Django

unread,
Aug 9, 2017, 7:22:31 AM8/9/17
to django-...@googlegroups.com
#28484: QuerySet.extra keyword
----------------------------------+--------------------------------------

Reporter: Antonio Carrasco | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.11
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Description changed by Antonio Carrasco:

Old description:

> Related to the #extra deprecation
> https://docs.djangoproject.com/en/1.11/ref/models/querysets/#extra
> Given this example:
>
> queryset = Model.objects.all()
> ordered_ids = "1,3,5,7"
> queryset.annotate(custom=RawSQL(f"SELECT FIELD(id, %s)",
> (ordered_ids,))).order_by("custom")
> queryset.extra(select={"custom": f"FIELD(id, {ordered_ids})"},
> order_by=[custom]).distinct()
>

> The result of both queries is different. But executing the
> .query.__str__() query in the Mysql native console, the result is the
> same.
> The result with #extra is ok (the same the native mysql console returns)
> but the result with annotate is wrong.

New description:

Related to the #extra deprecation
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#extra
Given this example:

queryset = Model.objects.all()
ordered_ids = "1,3,5,7"
queryset.annotate(custom=RawSQL(f"SELECT FIELD(id, %s)",
(ordered_ids,))).order_by("custom")
queryset.extra(select={"custom": f"FIELD(id, {ordered_ids})"},
order_by=[custom]).distinct()


The result of both queries is different. But executing the ".query" query


in the Mysql native console, the result is the same.
The result with #extra is ok (the same the native mysql console returns)
but the result with annotate is wrong.

--

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

Django

unread,
Aug 9, 2017, 6:35:17 PM8/9/17
to django-...@googlegroups.com
#28484: QuerySet.extra keyword
-------------------------------------+-------------------------------------

Reporter: Antonio Carrasco | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Database layer (models, ORM)


Old description:

> Related to the #extra deprecation
> https://docs.djangoproject.com/en/1.11/ref/models/querysets/#extra
> Given this example:
>
> queryset = Model.objects.all()
> ordered_ids = "1,3,5,7"
> queryset.annotate(custom=RawSQL(f"SELECT FIELD(id, %s)",
> (ordered_ids,))).order_by("custom")
> queryset.extra(select={"custom": f"FIELD(id, {ordered_ids})"},
> order_by=[custom]).distinct()
>

> The result of both queries is different. But executing the ".query" query


> in the Mysql native console, the result is the same.
> The result with #extra is ok (the same the native mysql console returns)
> but the result with annotate is wrong.

New description:

Related to the #extra deprecation
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#extra
Given this example:
{{{
queryset = Model.objects.all()
ordered_ids = "1,3,5,7"
queryset.annotate(custom=RawSQL(f"SELECT FIELD(id, %s)",
(ordered_ids,))).order_by("custom")
queryset.extra(select={"custom": f"FIELD(id, {ordered_ids})"},
order_by=['custom']).distinct()
}}}

The result of both queries is different. But executing the ".query" query


in the Mysql native console, the result is the same.
The result with #extra is ok (the same the native mysql console returns)
but the result with annotate is wrong.

--

Comment:

I'm unclear what the issue is here. It seems expected that the SQL will be
different since one queryset uses `distinct()` and the other doesn't.
Could you clarify why you believe Django is at fault?

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

Django

unread,
Aug 11, 2017, 3:56:24 PM8/11/17
to django-...@googlegroups.com
#28484: QuerySet.extra keyword
-------------------------------------+-------------------------------------

Reporter: Antonio Carrasco | Owner: nobody
Type: Uncategorized | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

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


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

Reply all
Reply to author
Forward
0 new messages