MongoEngine – Adding reverse_delete_rule on ListField of ReferenceField Gives NotRegistered Error

huangapple go评论74阅读模式
英文:

MongoEngine - Adding reverse_delete_rule on ListField of ReferenceField Gives NotRegistered Error

问题

我正在使用Flask + MongoEngine(0.22.1)。我有两种文档类型,在这两种类型中我没有循环依赖:

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))

当我将reverse_delete_rule设置为PULL、DENY、CASCADE、NULLIFY(除了默认的DO_NOTHING)时,我总是遇到这个错误:

mongoengine.errors.NotRegistered: `Idp` has not been registered in the document registry.
            Importing the document class automatically registers it, has it
            been imported?

我已经查看了一些在线答案,它们大多涉及到循环依赖(我看不出我有循环依赖),其他人建议使用Tenant.register_delete_rule(..., PULL)记法(我不知道如何应用于ListField...)

对于这个错误有什么建议吗?

英文:

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?

答案1

得分: 0

在浏览了所有的谷歌搜索结果后,我向ChatGPT提出了问题,提供的解决方案解决了这个问题:

您遇到的错误,mongoengine.errors.NotRegistered: 'Idp' 在文档注册表中未注册,通常发生在在关系字段中引用文档类之前未导入文档类的情况下。
在您的情况下,似乎在Tenant文档的idps字段中使用Idp文档类之前未导入或注册Idp类。要解决此问题,您需要确保在定义Tenant类之前导入或注册Idp类。

我需要在__init__.py中更改导入顺序。

英文:

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.

答案2

得分: 0

如果您想注册删除规则,并且您的文档尚未定义,您可以在定义文档之后像这样注册它:

class Doc1(Document):
    field = ListField(ReferenceField("LaterDefinedDocument"))

class LaterDefinedDocument(Document):
    ...这里添加一些字段...

LaterDefinedDocument.register_delete_rule(Doc1, 'field', NULLIFY)
英文:

If you want to register a delete rule and your document has not been defined yet, you can register it after defining your document like this:

class Doc1(Document):
    field = ListField(ReferenceField("LaterDefinedDocument"))

class LaterDefinedDocument(Document):
    ...some fields here...

LaterDefinedDocument.register_delete_rule(Doc1, 'field', NULLIFY)

huangapple
  • 本文由 发表于 2023年6月12日 21:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定