Scille / umongo

sync/async MongoDB ODM, yes.
MIT License
446 stars 63 forks source link

ReferenceField with a Document with non-mongo-ObjectId. #339

Closed jbkoh closed 3 years ago

jbkoh commented 3 years ago

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 3 years ago

I just found https://github.com/Scille/umongo/blob/96abcff8586bc30014338ebdd52f97cd12b671f7/umongo/fields.py#L319, so it seems to be an expected behavior. Is there any plan to make it more generic?

jbkoh commented 3 years ago

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!

lafrech commented 3 years ago

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.