diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index f7233305a7..0d7e05eb0a 100644
a
|
b
|
def test_subquery(self):
|
685 | 685 | Employee.objects.exclude(company_point_of_contact_set=None).values("pk"), |
686 | 686 | ) |
687 | 687 | |
| 688 | @skipUnlessDBFeature("can_distinct_on_fields") |
| 689 | def test_ordering_in_distinct_exists_subquery(self): |
| 690 | # Show that the inner Exists query |
| 691 | # respects the ordering when it is distinct |
| 692 | manager = Manager.objects.create(name="King Arthur") |
| 693 | Employee.objects.create( |
| 694 | manager=manager, |
| 695 | salary=10, |
| 696 | firstname="Robin", |
| 697 | lastname="The Not-So-Brave", |
| 698 | ) |
| 699 | lancelot = Employee.objects.create( |
| 700 | manager=manager, |
| 701 | salary=20, # Higher than Robin's |
| 702 | firstname="Lancelot", |
| 703 | lastname="The Brave", |
| 704 | ) |
| 705 | |
| 706 | highest_paid_per_manager_subquery = ( |
| 707 | Employee |
| 708 | .objects |
| 709 | .filter(manager__isnull=False) |
| 710 | .order_by("manager", "-salary") |
| 711 | .distinct("manager") |
| 712 | .filter(pk=OuterRef("pk")) |
| 713 | ) |
| 714 | qs = Employee.objects.filter(Exists(highest_paid_per_manager_subquery)) |
| 715 | self.assertEqual(qs.get(manager=manager), lancelot) |
| 716 | |
| 717 | |
688 | 718 | def test_subquery_eq(self): |
689 | 719 | qs = Employee.objects.annotate( |
690 | 720 | is_ceo=Exists(Company.objects.filter(ceo=OuterRef("pk"))), |