Opened 61 minutes ago
Last modified 55 minutes ago
#37480 assigned Cleanup/optimization
FieldError for annotation excluded by previous values() call incorrectly refers to unpromoted alias
| Reported by: | Jacob Walls | Owned by: | Django Sprints |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.1 |
| Severity: | Low | 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
#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:
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.