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.
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>
* 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>
* status: new => closed
* resolution: => needsinfo
--
Ticket URL: <https://code.djangoproject.com/ticket/28484#comment:3>