mirror of
https://github.com/django/django.git
synced 2025-09-18 08:20:10 +00:00
Refs #33308 -- Enabled explicit GROUP BY and ORDER BY aliases.
This ensures explicit grouping from using values() before annotating an aggregate function groups by selected aliases if supported. The GROUP BY feature is disabled on Oracle because it doesn't support it.
This commit is contained in:
parent
c58a8acd41
commit
04518e310d
7 changed files with 78 additions and 13 deletions
|
@ -12,7 +12,12 @@ from django.core.management import call_command
|
|||
from django.db import IntegrityError, connection, models
|
||||
from django.db.models.expressions import Exists, OuterRef, RawSQL, Value
|
||||
from django.db.models.functions import Cast, JSONObject, Upper
|
||||
from django.test import TransactionTestCase, modify_settings, override_settings
|
||||
from django.test import (
|
||||
TransactionTestCase,
|
||||
modify_settings,
|
||||
override_settings,
|
||||
skipUnlessDBFeature,
|
||||
)
|
||||
from django.test.utils import isolate_apps
|
||||
from django.utils import timezone
|
||||
|
||||
|
@ -405,6 +410,27 @@ class TestQuerying(PostgreSQLTestCase):
|
|||
expected,
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature("allows_group_by_refs")
|
||||
def test_group_by_order_by_aliases(self):
|
||||
with self.assertNumQueries(1) as ctx:
|
||||
self.assertSequenceEqual(
|
||||
NullableIntegerArrayModel.objects.filter(
|
||||
field__0__isnull=False,
|
||||
)
|
||||
.values("field__0")
|
||||
.annotate(arrayagg=ArrayAgg("id"))
|
||||
.order_by("field__0"),
|
||||
[
|
||||
{"field__0": 1, "arrayagg": [1]},
|
||||
{"field__0": 2, "arrayagg": [2, 3]},
|
||||
{"field__0": 20, "arrayagg": [4]},
|
||||
],
|
||||
)
|
||||
alias = connection.ops.quote_name("field__0")
|
||||
sql = ctx[0]["sql"]
|
||||
self.assertIn(f"GROUP BY {alias}", sql)
|
||||
self.assertIn(f"ORDER BY {alias}", sql)
|
||||
|
||||
def test_index(self):
|
||||
self.assertSequenceEqual(
|
||||
NullableIntegerArrayModel.objects.filter(field__0=2), self.objs[1:3]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue