[Django] #33429: Wrong results when using .values().distinct() with default ordering

3 views
Skip to first unread message

Django

unread,
Jan 10, 2022, 2:56:22 PM1/10/22
to django-...@googlegroups.com
#33429: Wrong results when using .values().distinct() with default ordering
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: nobody
Sołtysik |
Type: Bug | Status: new
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
[https://docs.djangoproject.com/en/4.0/ref/models/querysets/#distinct 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?

--
Ticket URL: <https://code.djangoproject.com/ticket/33429>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 10, 2022, 3:07:50 PM1/10/22
to django-...@googlegroups.com
#33429: Wrong results when using .values().distinct() with default ordering
-------------------------------------+-------------------------------------
Reporter: Adam Sołtysik | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Bug => Cleanup/optimization
* resolution: => duplicate


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33429#comment:1>

Reply all
Reply to author
Forward
0 new messages