If I have the following two models:
class User(Document):
...
class Profile(Document):
user = ReferenceField(reverse_delete_rule=CASCADE)
Does deleting a User instance delete its Profile? Does deleting its Profile delete the user?
There seams to be an error in the documentation:
class Employee(Document): ... profile_page = ReferenceField('ProfilePage', reverse_delete_rule=mongoengine.NULLIFY)The declaration in this example means that when an Employee object is removed, the ProfilePage that belongs to that employee is removed as well. If a whole batch of employees is removed, all profile pages that are linked are removed as well.
The code uses NULLIFY, but the explanation indicates the use of CASCADE. or am I misunderstanding something?
Deleting a User instance deletes its Profile. This is the way reverse_delete_rule=CASCADE works. Just like in Relational Databases.
You may check this code:
from mongoengine import connect, Document, ReferenceField, CASCADE
connect('test_cascade')
class User(Document):
pass
class Profile(Document):
user = ReferenceField(User, reverse_delete_rule=CASCADE)
user = User().save()
profile = Profile(user=user).save()
user.delete()
assert Profile.objects.count() == 0
They also updated documentation, now it's other way around:
class ProfilePage(Document): ... employee = ReferenceField('Employee', reverse_delete_rule=mongoengine.CASCADE)The declaration in this example means that when an Employee object is removed, the ProfilePage that references that employee is removed as well. If a whole batch of employees is removed, all profile pages that are linked are removed as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With