Opened 3 years ago

Closed 3 years ago

#33429 closed Cleanup/optimization (duplicate)

Wrong results when using .values().distinct() with default ordering

Reported by: Adam Sołtysik Owned by: nobody
Component: Database layer (models, ORM) Version: 4.0
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

The following test (added in tests/ordering/tests.py) fails, since columns from Meta.ordering are added to the query, essentially removing the effect of .values() and making the rows not distinct.

    def test_values_with_default_ordering(self):
        qs = Article.objects.values('author').distinct()
        self.assertEqual(qs.count(), len(qs.order_by()))  # OK
        self.assertEqual(qs.count(), len(qs))             # FAIL: 1 != 4

Even though this behaviour is documented, it's quite unexpected that the default ordering still impacts such queries, even after fixing #14357. In #32811 it has been noted that "the previous behavior [before #14357] was implicit, unexpected, and caused many support questions". Probably the same could be said in this case. So shouldn't this also be fixed?

Change History (1)

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: newclosed
Type: BugCleanup/optimization

Duplicate of #33382 and #30655. We need to reach a strong consensus on the DevelopersMailingList and deprecate the current (documented) behavior before we can move it forward. You can raise the idea on the DevelopersMailingList to reach a wider audience and see what other think.

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