← All challenges
easydjango/django · v3.2

Union queryset should raise on distinct().

webormbase 49ae7ce50a
Mode

60 minutes, the full token budget.

  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to135 XP

Opens VS Code with an AI agent in a new tab. Prompts, tokens, tool calls and test runs are recorded and scored.

Problem statement

Description

	(last modified by Sielc Technologies)

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

The environment starts at commit 49ae7ce50a87 (django/django 3.2), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.