hmarr/mongoengine — Issue #345: Circular references with EmbeddedDocumentField
Author: jonrscott
Created: 2011-10-31 17:27:15+00:00
State: open

Hi,

I had a search but couldn't find this particular issue. I have a schema which contains an embedded document which has further embedded documents in it, which reference the same embedded document type. This causes an infinite recursion in _unique_with_indexes on TopLevelDocumentMetaClass.

Here's a simple version of the schema which causes the issue:

class RecursiveObject(mongoengine.EmbeddedDocument):
obj = mongoengine.EmbeddedDocumentField('RecursiveObject')

class RecursiveDocument(mongoengine.Document):
recursiveobj = mongoengine.EmbeddedDocumentField(RecursiveObject)

I did a quick fix for this which doesn't recurse into _unique_with_indexes if the field is an EmbeddedDocumentField with the same document type as the current field, but not sure if that's ideal for general use:

(at the end of _unique_with_indexes):

if field.__class__.__name__ == "EmbeddedDocumentField" and field.document_type != new_class:

Cheers, Jon