Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two-way reference raise NotRegistered Exception when using reverse_delete_rule #1707

Closed
chenli0906 opened this issue Nov 27, 2017 · 2 comments

Comments

@chenli0906
Copy link

chenli0906 commented Nov 27, 2017

code as below:

class A(Document):
    name = StringField()
    ref_b= ListField(ReferenceField('B', reverse_delete_rule=PULL))

class B(Document):
    content = StringField()
    ref_a = ReferenceField('A', reverse_delete_rule=NULLIFY)

User Doc has a forward references Page, when i set a reverse_delete_rule other than DO_NOTHING,it will raise
mongoengine.errors.NotRegistered: `B` has not been registered in the document registry. Importing the document class automatically registers it, has it been imported?

I want to automatically delete reference when I delete document

how can i avoid this Exception?

@reasonman
Copy link

I worked around this by separating the delete rule for model that referenced the "unregistered" model. I had to register the delete rule for the model defined first(User in your example). Yours would look like this I think:

class User(Document):
    name = StringField()
    ref_page= ListField(ReferenceField('Page'))

class Page(Document):
    content = StringField()
    ref_user = ReferenceField(User, reverse_delete_rule=NULLIFY)

User.register_delete_rule(Page, "ref_page", PULL)

@chenli0906
Copy link
Author

@reasonman Yes,that’s it, I find it in API Reference, Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants