Opened 6 years ago
Closed 6 years ago
#31580 closed Cleanup/optimization (fixed)
Union queryset should raise on distinct().
| Reported by: | Sielc Technologies | Owned by: | Hasan Ramezani |
|---|---|---|---|
| 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 (last modified by )
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
Change History (6)
comment:1 by , 6 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 6 years ago
| Cc: | added |
|---|---|
| Easy pickings: | set |
| Summary: | Expect to get SELECT DISTINCT ON after UNION of 2 annotated QuerySet → Union queryset should raise on distinct(). |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
| Version: | 3.0 → master |
comment:3 by , 6 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 6 years ago
| Has patch: | set |
|---|
Note:
See TracTickets
for help on using tickets.
distinct()is not supported but doesn't raise an error yet. As per the documentation, "only LIMIT, OFFSET, COUNT(*), ORDER BY, and specifying columns (i.e. slicing, count(), order_by(), and values()/values_list()) are allowed on the resulting QuerySet.".Follow up to #27995.