diff --git a/docs/changelog.rst b/docs/changelog.rst index 7c63a160..6e1afaa5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Errors raised if trying to perform a join in a query - Updates can now take __raw__ queries - Added custom 2D index declarations - Added replicaSet connection support diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 6ebaacaa..3e4d4740 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -610,6 +610,9 @@ class QuerySet(object): raise InvalidQueryError('Cannot resolve field "%s"' % field_name) else: + from mongoengine.fields import ReferenceField, GenericReferenceField + if isinstance(field, (ReferenceField, GenericReferenceField)): + raise InvalidQueryError('Cannot perform join in mongoDB: %s' % '__'.join(parts)) # Look up subfield on the previous field new_field = field.lookup_member(field_name) from base import ComplexBaseField diff --git a/tests/document.py b/tests/document.py index fc9350b3..35734027 100644 --- a/tests/document.py +++ b/tests/document.py @@ -10,6 +10,7 @@ from fixtures import Base, Mixin, PickleEmbedded, PickleTest from mongoengine import * from mongoengine.base import NotRegistered, InvalidDocumentError +from mongoengine.queryset import InvalidQueryError from mongoengine.connection import get_db @@ -640,7 +641,7 @@ class DocumentTest(unittest.TestCase): location = DictField() meta = { 'indexes': [ - '*location.point', + '*location.point', ], } Place.drop_collection() @@ -2256,6 +2257,22 @@ class DocumentTest(unittest.TestCase): BlogPost.drop_collection() + def test_cannot_perform_joins_references(self): + + class BlogPost(Document): + author = ReferenceField(self.Person) + author2 = GenericReferenceField() + + def test_reference(): + list(BlogPost.objects(author__name="test")) + + self.assertRaises(InvalidQueryError, test_reference) + + def test_generic_reference(): + list(BlogPost.objects(author2__name="test")) + + self.assertRaises(InvalidQueryError, test_generic_reference) + def test_duplicate_db_fields_raise_invalid_document_error(self): """Ensure a InvalidDocumentError is thrown if duplicate fields declare the same db_field"""