Opened 6 years ago
Last modified 6 years ago
#31580 closed Cleanup/optimization
Expect to get SELECT DISTINCT ON after UNION of 2 annotated QuerySet — at Initial Version
| Reported by: | Sielc Technologies | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | orm, distinct, annotate, union |
| Cc: | Hasan Ramezani | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
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
Note:
See TracTickets
for help on using tickets.