#11256 closed (fixed)
Fail loudly and clearly when an Annotation alias matches a field name.
Reported by: | donspaulding | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Continued from a django-users thread
Given the following two models:
class Author(models.Model): last_updated = models.DateField() class Book(models.Model): author = models.ForeignKey(Author, related_name='books') pubdate = models.DateField()
The following annotation query will produce unexpected results:
authors = Author.objects.annotate(last_updated=Max('books__pubdate'))
And it may not raise an error until you combine it with other parts of the queryset api, like order_by:
authors = Author.objects.annotate(last_updated=Max('books__pubdate')).order_by('last_updated')
While it's true that these invalid queries are the developer's fault, it's not at all obvious what's gone wrong. Unless there is some valid case for clobbering your own namespace, I suggest that instead of leaving this behavior defined by the particular DB and query, an Exception be raised to let the developer know they've made a mistake before it hits the SQL level.
Attachments (3)
Change History (13)
comment:1 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
by , 15 years ago
Attachment: | aggregation-validationl.diff added |
---|
comment:2 by , 15 years ago
Has patch: | set |
---|
by , 15 years ago
Attachment: | aggregate-validation.diff added |
---|
comment:3 by , 14 years ago
milestone: | → 1.3 |
---|
#14373 reported this same issue, with the additional factor that under certain (not yet entirely clear) circumstances with m2m fields, this can lead to data loss. Bumping to 1.3 milestone.
comment:4 by , 14 years ago
New patch updates tests to apply post-doctest-removal, and modifies the duplicate-alias error message to be a bit more informative (tells you what alias is a dupe).
comment:6 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:7 by , 14 years ago
comment:8 by , 14 years ago
comment:10 by , 12 years ago
Component: | ORM aggregation → Database layer (models, ORM) |
---|
Agreed - an attempt to annotate using a name that already exists on the model should raise an exception at time of query construction.