Opened 8 years ago

Last modified 4 years ago

#28560 new Bug

distinct() on ordered queryset with restricted list of columns returns incorrect result — at Version 7

Reported by: Mariusz Felisiak Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: dev
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 Mariusz Felisiak)

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.

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (6)

comment:2 by Mariusz Felisiak, 7 years ago

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

comment:3 by Mariusz Felisiak, 7 years ago

Description: modified (diff)

comment:4 by Simon Charette, 7 years ago

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

comment:5 by Mariusz Felisiak, 7 years ago

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

comment:6 by Simon Charette, 7 years ago

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

comment:7 by Mariusz Felisiak, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top