Raise an error if trying to perform a join

You can't join across reference fields, so raise an error
if someone tries to.
This commit is contained in:
Ross Lawley 2012-02-29 10:10:51 +00:00
parent e9b8093dac
commit 2a391f0f16
3 changed files with 22 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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"""