I am using Flask + MongoEngine (0.22.1). I have 2 document types, where I don't have any circular dependency:
from mongoengine import Document, StringField, UUIDField, ListField
from openapi_crud_framework import MongoSettings
class Idp(Document):
meta = {
'auto_create_index': MongoSettings.auto_create_index()
}
uuid = UUIDField(binary=False, default=lambda: str(uuid4()), required=True, unique=True)
scopes = ListField(required=False, field=StringField(max_length=100))
from mongoengine import Document, UUIDField, ReferenceField, ListField, PULL
from openapi_crud_framework import MongoSettings
class Tenant(Document):
meta = {
'auto_create_index': MongoSettings.auto_create_index()
}
uuid = UUIDField(binary=False, default=lambda: str(uuid4()), required=True, unique=True)
idps = ListField(required=False, field=ReferenceField(document_type='Idp', reverse_delete_rule=PULL))
When I add reverse_delete_rule as PULL, DENY, CASCADE, NULLIFY (anything except the default DO_NOTHING) I am always having this error:
mongoengine.errors.NotRegistered: `Idp` has not been registered in the document registry.
Importing the document class automatically registers it, has it
been imported?
I have checked some answers online, and they're mostly about circular-dependency (which I don't see that I have) and others are suggesting to use Tenant.register_delete_rule(..., PULL)
notation (which I don't know how to apply for ListField...)
Any suggestions for this error?
After scrolling through all google results, I asked the question to ChatGPT, and the solution provided solved the issue:
The error you're encountering, mongoengine.errors.NotRegistered: 'Idp' has not been registered in the document registry, typically occurs when a document class has not been imported before it is referenced in a relationship field. In your case, it seems like the Idp document class is not being imported or registered before it is used in the Tenant document's idps field. To resolve this issue, you need to ensure that the Idp class is imported or registered before defining the Tenant class.
I needed to change the import order in __init__.py
.