#31580: Expect to get SELECT DISTINCT ON after UNION of 2 annotated QuerySet
-------------------------------------+-------------------------------------
Reporter: Sielc | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: orm, distinct, | Triage Stage:
annotate, union | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Sielc:
Old description:
> After using
> - .annotate() on 2 different querysets
> - and then .union()
> - .distinct() will not affect the queryset
>
> {{{
> def setUp(self) -> None:
> user = self.get_or_create_admin_user()
>
> Sample.h.create(user, name="Sam1")
> Sample.h.create(user, name="Sam2 acid")
> Sample.h.create(user, name="Sam3")
> Sample.h.create(user, name="Sam4 acid")
>
> Sample.h.create(user, name="Dub")
> Sample.h.create(user, name="Dub")
> Sample.h.create(user, name="Dub")
>
> self.user = user
>
> def test_union_annotated_diff_distinct(self):
> qs = Sample.objects.filter(user=self.user)
> qs1 = qs.filter(name='Dub').annotate(rank=Value(0,
> IntegerField()))
> qs2 = qs.filter(name='Sam1').annotate(rank=Value(1,
> IntegerField()))
> qs = qs1.union(qs2)
> qs = qs.order_by('name').distinct('name') # THIS DISTINCT
> DONESN'T WORK
> self.assertEqual(qs.count(), 2)
> }}}
>
> expected to get wrapped union
> {{{
> SELECT DISTINCT ON (siebox_sample.name) * FROM (SELECT ... UNION
> SELECT ...) AS siebox_sample
> }}}
New description:
After using
- .annotate() on 2 different querysets
- and then .union()
- .distinct() will not affect the queryset
{{{
def setUp(self) -> None:
user = self.get_or_create_admin_user()
Sample.h.create(user, name="Sam1")
Sample.h.create(user, name="Sam2 acid")
Sample.h.create(user, name="Sam3")
Sample.h.create(user, name="Sam4 acid")
Sample.h.create(user, name="Dub")
Sample.h.create(user, name="Dub")
Sample.h.create(user, name="Dub")
self.user = user
def test_union_annotated_diff_distinct(self):
qs = Sample.objects.filter(user=self.user)
qs1 = qs.filter(name='Dub').annotate(rank=Value(0,
IntegerField()))
qs2 = qs.filter(name='Sam1').annotate(rank=Value(1,
IntegerField()))
qs = qs1.union(qs2)
qs = qs.order_by('name').distinct('name') # THIS DISTINCT DOESN'T
WORK
self.assertEqual(qs.count(), 2)
}}}
expected to get wrapped union
{{{
SELECT DISTINCT ON (siebox_sample.name) * FROM (SELECT ... UNION
SELECT ...) AS siebox_sample
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31580#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.011add8a6539017eadb92f8704b498bc%40djangoproject.com.