mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #19259 -- Added group by selected primary keys support.
This commit is contained in:
@@ -7,10 +7,11 @@ from operator import attrgetter
|
||||
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import FieldError
|
||||
from django.db import connection
|
||||
from django.db.models import (
|
||||
F, Q, Avg, Count, Max, StdDev, Sum, Value, Variance,
|
||||
)
|
||||
from django.test import TestCase, skipUnlessDBFeature
|
||||
from django.test import TestCase, skipUnlessAnyDBFeature, skipUnlessDBFeature
|
||||
from django.test.utils import Approximate
|
||||
from django.utils import six
|
||||
|
||||
@@ -1011,7 +1012,7 @@ class AggregationTests(TestCase):
|
||||
# Check that the query executes without problems.
|
||||
self.assertEqual(len(qs.exclude(publisher=-1)), 6)
|
||||
|
||||
@skipUnlessDBFeature("allows_group_by_pk")
|
||||
@skipUnlessAnyDBFeature('allows_group_by_pk', 'allows_group_by_selected_pks')
|
||||
def test_aggregate_duplicate_columns(self):
|
||||
# Regression test for #17144
|
||||
|
||||
@@ -1041,7 +1042,7 @@ class AggregationTests(TestCase):
|
||||
]
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature("allows_group_by_pk")
|
||||
@skipUnlessAnyDBFeature('allows_group_by_pk', 'allows_group_by_selected_pks')
|
||||
def test_aggregate_duplicate_columns_only(self):
|
||||
# Works with only() too.
|
||||
results = Author.objects.only('id', 'name').annotate(num_contacts=Count('book_contact_set'))
|
||||
@@ -1067,13 +1068,14 @@ class AggregationTests(TestCase):
|
||||
]
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature("allows_group_by_pk")
|
||||
@skipUnlessAnyDBFeature('allows_group_by_pk', 'allows_group_by_selected_pks')
|
||||
def test_aggregate_duplicate_columns_select_related(self):
|
||||
# And select_related()
|
||||
results = Book.objects.select_related('contact').annotate(
|
||||
num_authors=Count('authors'))
|
||||
_, _, grouping = results.query.get_compiler(using='default').pre_sql_setup()
|
||||
self.assertEqual(len(grouping), 1)
|
||||
# In the case of `group_by_selected_pks` we also group by contact.id because of the select_related.
|
||||
self.assertEqual(len(grouping), 1 if connection.features.allows_group_by_pk else 2)
|
||||
self.assertIn('id', grouping[0][0])
|
||||
self.assertNotIn('name', grouping[0][0])
|
||||
self.assertNotIn('contact', grouping[0][0])
|
||||
|
||||
Reference in New Issue
Block a user