Fix bug: rename annotation_mask #61
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Abstract
When the name of an existing field on the model is used as the alias name,
annotate
function renames the fields.django-postgres-extra/psqlextra/manager/manager.py
Lines 46 to 57 in f4ce6ad
After that,
rename_annotations
renames the fields to the original name again.django-postgres-extra/psqlextra/query.py
Lines 34 to 44 in 76b6860
However,
annotation_select_mask
is not changed.As a result, the renamed fields are lost because
_annotation_select_cache
is generated byannotation_select_mask
.https://github.com/django/django/blob/952f05a6db2665d83c04075119285f2164b03432/django/db/models/sql/query.py#L2022-L2027
In addition, though
self.annotations
isOrderedDict
, order is destroyed in the following code.django-postgres-extra/psqlextra/query.py
Lines 43 to 44 in 76b6860
Sample
https://github.com/knqyf263/psqlextra_fix
In
test_annotation_bug
,description
is not selected correctly.https://github.com/knqyf263/psqlextra_fix/blob/master/users/tests.py#L10-L14
In
test_union_exception
,ValueError
exception is thrown.This is because the number of fields does not match the number of converters due to
annotation_select_mask
bug.https://github.com/knqyf263/psqlextra_fix/blob/master/users/tests.py#L16-L23
Fix
self.annotations
.self.annotation_select_mask
.I'm not sure if this fix is correct, please review me!