mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

ReferenceField ValidationError

Open vedavidhbudimuri opened this issue 9 years ago • 6 comments

Error: ValidationError: ValidationError (B:59b64497faa82030ee78ef93) (A ReferenceField only accepts DBRef, ObjectId or documents: ['ref_field_a'])

Error is trigger at save method call.

How to reproduce the error

    from mongoengine import Document
    from mongoengine import StringField
    from mongoengine import ReferenceField

    class A(Document):
        field_1 = StringField(primary_key=True)

    class B(Document):
        ref_field_a = ReferenceField(A)

    A.objects.delete()
    B.objects.delete()

    a1 = A(field_1='one')
    a1.save()

    assert a1.id == a1.field_1 == 'one'

    a2 = A(field_1='two')
    a2.save()

    b = B(ref_field_a=a1)
    b.save()

Case 1

    assert b.ref_field_a == a1
    b.ref_field_a = a2.field_1 
    b.save()
    b = B.objects(id=b.id)[0]
    assert b.ref_field_a == a2

Case 2

    assert b.ref_field_a == a1
    b.ref_field_a = a2.id 
    b.save()
    b = B.objects(id=b.id)[0]
    assert b.ref_field_a == a2

Both Case 1 and Case 2 are failing. Where as below snippet is working fine

Case 3

    from mongoengine import Document
    from mongoengine import StringField
    from mongoengine import ReferenceField

    class A(Document):
        field_1 = StringField()

    class B(Document):
        ref_field_a = ReferenceField(A)

    A.objects.delete()
    B.objects.delete()

    a1 = A(field_1='one')
    a1.save()

    assert a1.field_1 == 'one'

    a2 = A(field_1='two')
    a2.save()

    b = B(ref_field_a=a1)
    b.save()

    assert b.ref_field_a == a1
    b.ref_field_a = a2.id
    b.save()
    b = B.objects(id=b.id)[0]

    assert b.ref_field_a == a2

vedavidhbudimuri avatar Sep 11 '17 08:09 vedavidhbudimuri

This is in the nature of validator

https://github.com/MongoEngine/mongoengine/blob/5bdd35464b89dd2def1dcb0464e244322f1bc757/mongoengine/fields.py#L1088-L1091

daxlab avatar Sep 17 '17 17:09 daxlab

Sorry for the inconvenience, but my assumption in Case 2 is that a2.id will return an objectId(Which is accepted). If my assumption is wrong can you tell me how to fetch the objectId?

I am new to mongodb and mongoengine, I have just referred to https://mongoengine-odm.readthedocs.io/guide/document-instances.html for document instances.

Thanks in advance.

vedavidhbudimuri avatar Sep 18 '17 08:09 vedavidhbudimuri

@vedavidhbudimuri can elaborate a little on your motive ?

daxlab avatar Sep 18 '17 08:09 daxlab

@daxlab, The scenario is that sometimes I might only have primary key of 'A' document and need to create instance of 'B'document, so I thought of passing the objectId instead of 'A' document instance(as it worked fine when I haven't mentioned any primary key i.e Case 3 ).

vedavidhbudimuri avatar Sep 18 '17 09:09 vedavidhbudimuri

@vedavidhbudimuri you have to pass objects only instead of id.

daxlab avatar Sep 18 '17 09:09 daxlab

Thank you @daxlab. Aren't Case 2 and Case 3 the same (except that Document 'A' doesn't have primary key in Case 2)? and as you said we can pass ObjectId, how can I do that when I have defined a primary key (as obj.id is working out)?

vedavidhbudimuri avatar Sep 18 '17 09:09 vedavidhbudimuri