Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key error when annotate() for child model applied to QuerySet #1

Open
epicbagel opened this issue Mar 9, 2011 · 1 comment
Open

Comments

@epicbagel
Copy link

When adding annotate to a QuerySet that is to be cached, it throws a Key Error exception (see below - CommentAttachment has a foreign key point to a Comment model). It works find if counting within the same model, but not with a related model (e.g. through the foreign key)

Note: The error only occurs the first time the query is run.

Comment.objects.cache().filter(id = 1).annotate(Count('commentattachment'))
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 68, in repr
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 83, in len
self._result_cache.extend(list(self._iter))
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 434, in iterator
for obj in CacheBot(self):
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 65, in iter
self.cache_results(results)
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 107, in cache_results
invalidation_dict.update(dict([(key, self.result_key) for key in self.get_invalidation_keys(results)]))
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 159, in get_invalidation_keys
related_fields = self.queryset._related_fields
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 204, in _related_fields
for attname, model_class in self._get_related_models(self.model):
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 227, in _get_related_models
for join_attname, model_klass in self._get_related_models(model_class):
File "/usr/lib/python2.6/dist-packages/cachebot/queryset.py", line 217, in _get_related_models
related_models.add((rev_reversemapping[attname], related.model))
KeyError: 'update_set'
Comment.objects.cache().filter(id = 1).annotate(Count('commentattachment'))
[]

@braskin
Copy link

braskin commented Apr 10, 2011

I have a similar issue. In my case, somehow the auth_user table sneaks into the list of tables for the query and then _get_related_models goes through the reverse relations for the auth_user model and eventually craps out. I don't really get it why auth_user gets returned in the self.query.tables call. Something is wrong. If you have a fix for this, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants