Opened 5 years ago

Closed 5 years ago

#31152 closed Bug (duplicate)

Annotating ArrayAgg and Subquery crashes with values().

Reported by: Ivan Vasić Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Think I found a regression issue that does not happen with Django <3.0 and does in the latest 3.x releases.

Book.objects
            .annotate(
                labels=ArrayAgg(
                    "reviews__labels",
                    filter=Q(reviews__labels__isnull=False),
                    distinct=True,
                )
            )
            .annotate(
                fav_count=Subquery(
                    FavoriteBook.objects.filter(book=OuterRef("pk"))
                    .annotate(count=Count("id"))
                    .values("count")[:1],
                    output_field=IntegerField(),
                )
            )
           .values('id')

This crashes with

django.db.utils.ProgrammingError: column "fav_count" does not exist
LINE 1: ..._labels"."review_id") GROUP BY "myapp_book"."id", "fav_count...
                                                             ^

(Disregard that it makes no sense to do annotations if we're only using "id" values - it's for the purpose of this example)

Asking for .values('fav_count') works though. I've compiled a small example project that contains all the models and a test case that reproduces this issue.

Attachments (1)

proj.zip (8.7 KB ) - added by Ivan Vasić 5 years ago.
Project

Download all attachments as: .zip

Change History (2)

by Ivan Vasić, 5 years ago

Attachment: proj.zip added

Project

comment:1 by Mariusz Felisiak, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed
Summary: Annotating ArrayAgg and Subquery crashesAnnotating ArrayAgg and Subquery crashes with values().

Duplicate of #31136.

Note: See TracTickets for help on using tickets.
Back to Top