Opened 10 years ago

Closed 10 years ago

#24642 closed Bug (invalid)

If queryset has orderby before distinct query selects the orderby

Reported by: Esteban F. Owned by: nobody
Component: Database layer (models, ORM) Version: 1.7
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)

If you generate a query set like:

orders = Order.objects.filter( date = paramDate, objfilter = paramObjfilter ).order_by( 'date' )
dudes  = orders.values('dude').distinct().exclude(dude__isnull = True) 

Query will be:

SELECT DISTINCT "base_ordentrabajo"."dude_id", "base_orders"."date" FROM "base_orders" WHERE ...

If you remove order_by and make distinct after query is quite different (and like should be)

orders = Order.objects.filter( date = paramDate, objfilter = paramObjfilter )
dudes  = orders.values('dude').distinct().exclude(dude__isnull = True) 

SELECT DISTINCT "base_ordentrabajo"."dude_id" FROM "base_orders" WHERE ...

So i can't make an order_by if i need to make a distinct first. Anyway django should warn of query.order_by or don't allow to make a subset query filter with distinct if you order in the first place because this lead to unexpected results on query (query will contain not distinct values for the field you are searching for because it adds the order_by field value to the query)

Change History (2)

comment:1 by Tim Graham, 10 years ago

Description: modified (diff)

comment:2 by Tim Graham, 10 years ago

Resolution: invalid
Status: newclosed

This is a documented behavior, please see the first note box under the distinct() documentation.

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