Opened 7 years ago

Last modified 7 years ago

#28484 closed Uncategorized

QuerySet.extra keyword — at Version 2

Reported by: Antonio Carrasco Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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.

Change History (2)

comment:1 by Antonio Carrasco, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Component: UncategorizedDatabase layer (models, ORM)
Description: modified (diff)

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?

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