Skip to content


assertion failure when raw_query() with certain filters is followed by count() prior to evaluating QuerySet #112

ablegrape opened this Issue · 2 comments

3 participants


Versions: django_mongodb_engine: 0.4.0, MongoDB: 2.0.3, Python 2.7.1, MacOS 10.6.8.

This seems to be 100% reproducible with the following steps.

  1. Make any raw_query()
  2. filter() the results with an operator like __lt or __gt
  3. use count() on the resulting QuerySet
  4. try to evaluate the query set (e.g. with list(qs) or qs[subscript])

Boom! Assertion failure.

Here's an example (backslashes added to help GitHub display this nicely):

>>> qs=DataRecord.objects.raw_query({'some_field' : 430}).filter(other_field__lt=6)
>>> qs.count()
>>> qs[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mypath/nonrel/lib/python2.7/site-packages/django/db/models/", line 190, in __getitem__
    return list(qs)[0]
  File "/mypath/nonrel/lib/python2.7/site-packages/django/db/models/", line 84, in __len__
  File "/mypath/nonrel/lib/python2.7/site-packages/django/db/models/", line 275, in iterator
    for row in compiler.results_iter():
  File "/mypath/nonrel/lib/python2.7/site-packages/djangotoolbox/db/", line 229, in results_iter
    for entity in self.build_query(fields).fetch(low_mark, high_mark):
  File "/mypath/nonrel/lib/python2.7/site-packages/djangotoolbox/db/", line 289, in build_query
  File "/mypath/nonrel/lib/python2.7/site-packages/django_mongodb_engine/", line 159, in add_filters
    self.add_filters(child, query=subquery)
  File "/mypath/nonrel/lib/python2.7/site-packages/django_mongodb_engine/", line 246, in add_filters
    assert all(key not in existing for key in lookup.keys()), [lookup, existing]
AssertionError: [{'$lt': 6}, {'$lt': 6}]
@jonashaag jonashaag added a commit that referenced this issue
@jonashaag jonashaag Tests for #112 e0cf493

Thanks for the report! I don't have time to fix this right now, but I added some tests that reproduce the issue (and another issue I stumbled upon).

Probably has something to do with mutable datastructures in the compiler.


@ablegrape Do you have a Decimal field defined on your model and you're trying to run filter on it?
I have the condition and I'm wondering yours is same to mine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.