mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Lazy Reference to Abstract class attempts to obtain the classes `id_field`

Open david-alm opened this issue 4 years ago • 2 comments

This is a similar issue to #837, though pertaining to LazyReferenceFields as opposed to ReferenceFields

The following code should enable you to replicate the issue in mongoengine version 0.18.0

import mongoengine as me

class AbstractExample(me.Document):
    meta = {"abstract": True}

class ConcreteExample(AbstractExample):
    pass

class Referrer(me.Document):
    reference = me.LazyReferenceField(AbstractExample)

Saving a Referrer to the database will trigger a KeyError: id_field. Replacing the LazyReferenceField with a regular ReferenceField doesn't trigger this exception.

david-alm avatar Jul 21 '19 09:07 david-alm

Here's another example, with a self-reference to an abstract class. Here the KeyError: id_field is thrown in the delete

from mongoengine import  *

class B(Document):
    a_id = LazyReferenceField('self', reverse_delete_rule=DENY)
    meta = {"allow_inheritance": True, "abstract": True}

class C(B):
    nc = IntField() 
    meta = {"allow_inheritance": False, "abstract": False}

connect('test', host='mongomock://localhost')

c1 = C(nc=3).save()
print(f"saved : {c1.to_mongo()}")
c1.delete()

Using mongoengine 0.19.1 , Python 3.7

Perhaps the error is actually allowing references to Abstract classes?

leonbloy avatar Jan 17 '20 02:01 leonbloy

We are also hitting up against this.

scottbelden avatar Jul 08 '22 10:07 scottbelden