Scille/umongo

ReferenceField with a Document with non-mongo-ObjectId.

jbkoh opened this issue · 3 comments

jbkoh commented

Hi again,

I have a Document whose ids are string. I would like to let another Document to Reference it like the following:

class AAA(Document):
    id = StringFiled(attribute='_id')

class BBB(Document):
    aaa = ReferenceField(AAA)

aaa_instance = AAA(id="abcd")
bbb_instance = BBB(aaa=aaa_instance.id)

However the last line gives me an error marshmallow.exceptions.ValidationError: Invalid ObjectId.

I was actually expecting ReferenceField's field to receive the document instances asthe argument (i.e., BBB(aaa=aaa_instance)), but umongo seems to interpret ReferenceField as ObjectIdField.

Is it an expected behavior or a bug? Thanks!

jbkoh commented

I just found

.. warning:: The referenced document's _id must be an `ObjectId`.
, so it seems to be an expected behavior. Is there any plan to make it more generic?

jbkoh commented

I looked through the code a bit and thought we could let Document to have other types than ObjectId as pk types. Maybe I'm wrong. Let me know if it's something worth investigating. Thanks!

Documents may have other types than ObjectId for id but there is a bug preventing them from being used in a ReferenceField.

This is a known issue. Closing this to let the discussion take place in #116.