mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Query transform fails when chaining filters for the same field if one is a DBRef

Open anemitz opened this issue 12 years ago • 3 comments

Forgive the lack of a better name for this issue.

A query in the example below should correctly transform.

>>> Lead.objects.filter(organization__in=['orga_ly5LOucvyHVQEZsoIXIcPzR26It3NOEuwE0GfG7ixmt', 'orga_po2P8yCOprDXxgT0BLXnZcLPR7fzasLz79oIp8P3M1T']).filter(organization__exact='orga_po2P8yCOprDXxgT0BLXnZcLPR7fzasLz79oIp8P3M1T')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 1842, in __repr__
    data.append(self.next())
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 965, in next
    return self._document._from_son(self._cursor.next())
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 583, in _cursor
    self._cursor_obj = self._collection.find(self._query,
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 379, in _query
    self._mongo_query = self._query_obj.to_query(self._document)
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 205, in to_query
    query = query.accept(QueryCompilerVisitor(document))
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 270, in accept
    return visitor.visit_query(self)
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 162, in visit_query
    return QuerySet._transform_query(self.document, **query.query)
  File "/Users/anemitz/Dropbox/projects/closeio/venv/lib/python2.7/site-packages/mongoengine/queryset.py", line 769, in _transform_query
    mongo_query[key].update(value)
TypeError: 'DBRef' object is not iterable

The values at line 769 of queryset.py are as follows.

op: exact
key:  organization
value:  DBRef('organization', u'orga_po2P8yCOprDXxgT0BLXnZcLPR7fzasLz79oIp8P3M1T')
mongo_query:  {
    'organization': 
        {'$in': 
            [DBRef('organization', u'orga_po2P8yCOprDXxgT0BLXnZcLPR7fzasLz79oIp8P3M1T'), DBRef('organization', u'orga_ly5LOucvyHVQEZsoIXIcPzR26It3NOEuwE0GfG7ixmt')]
        }
    }

anemitz avatar Jul 27 '12 17:07 anemitz

can you provide a test case?

rozza avatar Jul 30 '12 10:07 rozza

just chasing this up.

rozza avatar Aug 01 '12 12:08 rozza

Sorry for the delay. I have a fix + test case, but haven't gotten around to cleaning it up and making the pull request. Shooting for EOD tomorrow.

anemitz avatar Aug 01 '12 16:08 anemitz