Opened 3 months ago

Last modified 4 days ago

#28560 new Bug

distinct() on ordered queryset with restricted list of columns returns incorrect result

Reported by: felixxm Owned by:
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: distinct values
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by felixxm)

When distinct() is used with values() (or values_list()) on ordered queryset and a list of fields in values() doesn't contain all fields from ORDER BY, then it doesn't return correct result because columns from ORDER BY clause must be included in SELECT. As Simon suggested we should wrap a query in a subquery (see related tickets #7070, #5321).

Change History (11)

comment:2 Changed 3 months ago by felixxm

Summary: distinct() on None valuesdistinct() on ordered queryset with restricted list of columns returns incorrect result

comment:3 Changed 3 months ago by felixxm

Description: modified (diff)

comment:4 Changed 3 months ago by Simon Charette

Couldn't we wrap the query in a subquery like we did in #24254 in this case?

comment:5 Changed 3 months ago by felixxm

Good idea! it should be feasible. I will try to prepare patch in this week.

comment:6 Changed 3 months ago by Simon Charette

By the way Mariusz, do you have an opinion on https://code.djangoproject.com/ticket/14357#comment:11?

comment:7 Changed 2 months ago by felixxm

Description: modified (diff)

comment:8 Changed 2 months ago by felixxm

Description: modified (diff)

comment:9 Changed 2 months ago by felixxm

Has patch: set

comment:10 Changed 8 weeks ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:11 Changed 13 days ago by Tim Graham

Patch needs improvement: set

comment:12 Changed 4 days ago by felixxm

Has patch: unset
Owner: felixxm deleted
Patch needs improvement: unset
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top