possible issue in combination of SchemaDocument and use_autorefs

19 views
Skip to first unread message

Hervé Ménager

unread,
Mar 18, 2013, 10:09:06 AM3/18/13
to mong...@googlegroups.com
Hi,

I am currently trying to create a document with a pretty complex nested structure (named DocC for instance):
- Some parts of the nested structure are exclusive to the document so I modelized them as SchemaDocument classes (DocB), 
- other items are referenced by multiple documents, so I referenced them as separately stored documents (DocA). 

The issue I have is that when I try to save the resulting structure using autorefs I get an error. The test.py illustrating example attached results in an AttributeError such as this one:
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    dc.save()
  File "/local/lib/python2.7/site-packages/mongokit/document.py", line 410, in save
    self.validate(auto_migrate=False)
  File "/local/lib/python2.7/site-packages/mongokit/document.py", line 228, in validate
    self._make_reference(self, self.structure)
  File "/local/lib/python2.7/site-packages/mongokit/document.py", line 679, in _make_reference
    doc[key].save()
  File "/local/lib/python2.7/site-packages/mongokit/schema_document.py", line 379, in __getattr__
    return dict.__getattribute__(self, key)
AttributeError: 'DocB' object has no attribute 'save'
which leads me to think that when using autorefs mongokit also tries to save the SchemaDocument structures separately?

Commenting use_autorefs will solve the exception problem but then DocB is embedded in DocC.

Did I miss something? Is this a bug, a limitation, or a lack of understanding from me?

Regards,

Hervé
test.py

Nicolas Clairon

unread,
Mar 18, 2013, 11:04:20 AM3/18/13
to mong...@googlegroups.com
Hi,

To use auto_refs, you have to work with Document and not SchemaDocument. If you don't want DocB to be a Document, then use a regular dict in your structure:

class DocC(Document):
    use_autorefs = True
    __database__ = 'test'
    __collection__ = 'docc'
    structure = {'ca':DocA, 'cb':{"b": int"}}


--
 
---
You received this message because you are subscribed to the Google Groups "MongoKit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongokit+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hervé Ménager

unread,
Mar 18, 2013, 12:07:33 PM3/18/13
to mong...@googlegroups.com
Hi,

Thanks a lot for your quick answer. Actually my point is that:
- I don't want DocB to be a document, it makes poor sense from a mongo point of view
- I'd rather define it as a SchemaDocument than a dict, because my actual usecase is more complicated than the illustrating example: DocB will be the type of multiple properties in DocC.
This example, while still simplified, is a little closer to what I am defining:

class DocC(Document): 
    use_autorefs = True 
    __database__ = 'test' 
    __collection__ = 'docc' 
    structure = {'ca': DocA, 
                      'cbx': [DocB],
                      'cby': [DocB]
                     }

Working this way would give me a much more powerful and flexible way to define and maintain such a schema. Therefore I think than rather than removing DocB class, I won't use use_autorefs flag.

Best,

Nicolas Clairon

unread,
Mar 18, 2013, 12:10:02 PM3/18/13
to mong...@googlegroups.com
you can then make DocB as a dict:

DocB = {"b": int}

class DocC(Document): 
    use_autorefs = True 
    __database__ = 'test' 
    __collection__ = 'docc' 
    structure = {'ca': DocA, 
                      'cbx': [DocB],
                      'cby': [DocB]
                     }

This will work and will be reusable as well.

Hervé Ménager

unread,
Mar 18, 2013, 12:26:21 PM3/18/13
to mong...@googlegroups.com
Ok, thanks. It seems to be a suitable solution, to be honest I hadn't
thought about it.

As a more general rule, when would you recommend the use of the
SchemaDocument class over a dictionary template like the one you
mention?
> You received this message because you are subscribed to a topic in the
> Google Groups "MongoKit" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/mongokit/110IunJSRxk/unsubscribe?hl=en.
> To unsubscribe from this group and all its topics, send an email to

Nicolas Clairon

unread,
Mar 19, 2013, 4:48:58 AM3/19/13
to mong...@googlegroups.com
SchemaDocument is useful if you want to have a validated dict structure without interaction with any databases. Its purpose was to be able to add mongokit layer on top of another schemaless storages like ElasticSearch.

If you want to interact with mongodb, you should definitely use Document and forget SchemaDocument.

Where did you learn about SchemaDocument by the way ? I dont remember having writing about it in the documentation...
Reply all
Reply to author
Forward
0 new messages