Opened 16 years ago
Last modified 12 years ago
#10766 closed
Confusing error message on invalid annotate aggregate field — at Version 1
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.0 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I recently wrote the following code:
classes = classes.annotate(num_students=Count('anchor__child_set__userbit_qsc')) classes = classes.annotate(total_max_student=Sum('sections__num_students'))
The second line above (correctly) generated an error for me because the model referenced by "sections" doesn't contain a field named "num_students".
However, the error message returned by the exception was as follows:
Cannot resolve keyword 'num_students' into field. Choices are: [long valid list of fields], num_students
This confused me for a good while (until I realized that it was my mistake for using the wrong field name). On experimentation, the "num_students" in the list of fields does in fact come from the first annotate call.
It's hardly a critical failure; but it seems like poor form to declare a field as "invalid" then promptly list it as one of the valid options... More generally, it seems like that list just lists all annotated fields, which is only correct if you're not doing a join first.
I suspect that the fix is on line 1678 or so of django/db/models/sql/query.py (in particular, maybe it needs to use a different/new function on the Options class to get the keys properly?); but I don't know enough of those internals to know that I'm doing the right thing there. I could work up a patch that does this if no one more-experienced feels like it or knows of a good reason not to.
Please use preview :)