My Group By Aggregations are Breaking in 5.2

When I upgraded to 5.2 today, a number of my query sets broke tracebacks on building the query. I didn’t notice anything in the BIC of the release notes. Before I file a proper ticket, I wanted to make sure I wasn’t just doing something unsupported or strange–despite it working fine until 5.2.

I tossed together a project to make it easier to clone and repro:

But in summary, when I have some models like:

from django.db import models


class Bag(models.Model):
    kind = models.PositiveBigIntegerField(unique=True)


class Order(models.Model):
    blended_at = models.DateTimeField(null=True, default=None)
    gross_weight = models.FloatField(null=True, default=None)
    tare_weight = models.FloatField(null=True, default=None)
    total_tons = models.FloatField(null=True, default=None)
    blend_status = models.CharField(max_length=2, blank=True)
    bag_type = models.ForeignKey(Bag, on_delete=models.CASCADE, null=True)

and try to build a queryset like:

from django.db.models import Case, When, F, FloatField, Value, Sum

from .models import Order


TONS_ANNOTATION = dict(
    blended_tons=Case(
        When(
            gross_weight__gt=0,
            then=(F("gross_weight") - F("tare_weight")) / Value(2000)
        ),
        default=F("total_tons"),
        output_field=FloatField()
    )
)

dry_tons = Order.objects.filter(
    blended_at__date__range=["2025-03-01", "2025-03-31"],
).annotate(
    **TONS_ANNOTATION
).values(
    "blend_status",
    "blended_at__date",
    "bag_type__kind",
).annotate(
    tons=Sum("blended_tons")
)

I get the following traceback when trying to just print the query out:

>>> print(dry_tons.query)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.13/site-packages/django/db/models/sql/query.py", line 342, in __str__
    sql, params = self.sql_with_params()
                  ~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/sql/query.py", line 350, in sql_with_params
    return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 765, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup(
                                       ~~~~~~~~~~~~~~~~~~^
        with_col_aliases=with_col_aliases or bool(combinator),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 85, in pre_sql_setup
    self.setup_query(with_col_aliases=with_col_aliases)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 74, in setup_query
    self.select, self.klass_info, self.annotation_col_map = self.get_select(
                                                            ~~~~~~~~~~~~~~~^
        with_col_aliases=with_col_aliases,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 286, in get_select
    expression = cols[expression]
                 ~~~~^^^^^^^^^^^^
IndexError: tuple index out of range

This seems like I should file a bug on this but this doesn’t seem super crazy so I naturally think I’ve been doing something wrong and it just caught up with me.

Thoughts?

Went ahead and filed a ticket: #36292 (Group By Aggregations are Breaking in 5.2) – Django

1 Like