Opened 3 weeks ago

Last modified 4 days ago

#37480 assigned Cleanup/optimization

FieldError for annotation excluded by previous values() call incorrectly refers to unpromoted alias

Reported by: Jacob Walls Owned by: Stephen kihuni
Component: Database layer (models, ORM) Version: 6.1
Severity: Low Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

#36352 improved the FieldError message text when an annotation alias is requested via values() after being excluded by an earlier values() call.

However, it doesn't handle the case where the prior values call excludes all annotations, as in the last example below:

​DryORM fiddle

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)


def run():
    # qs = (
    #     Person.objects.annotate(a=models.Value(42), b=models.Value(43))
    #     .values("a")
    #     # CORRECT:
    #     # FieldError: Cannot select the 'b' alias. It was excluded by a
    #     # previous values() or values_list() call. Include 'b' in that
    #     # call to select it.
    #     .values("b")
    # )
    
    qs = (
        Person.objects.annotate(a=models.Value(42), b=models.Value(43))
        .values("pk")
        # INCORRECT:
        # FieldError: Cannot select the 'b' alias. Use annotate() to promote it.
        .values("b")
    )

I think we could check the contents of self.annotations (the annotations that have been registered) and self.annotation_select (the annotations that have been selected by a prior values() call) instead of only checking whether one or the other is empty.

Change History (5)

comment:1 by Jacob Walls, 3 weeks ago

Type: Bug → Cleanup/optimization

comment:2 by ontowhee, 3 weeks ago

Triage Stage: Unreviewed → Accepted

comment:3 by Stephen kihuni, 3 weeks ago

Owner: changed from Django Sprints to Stephen kihuni

comment:4 by Stephen kihuni, 4 days ago

comment:5 by Stephen kihuni, 4 days ago

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