Which direction does mongoengine reverse_delete_rule take?

If I have two following models:

class User(Document):
    ...

class Profile(Document):
    user = ReferenceField(reverse_delete_rule=CASCADE)

      

Does the custom instance delete their profile? Does it delete its user profile?

There are seams in the plugin :

class Employee(Document):
    ...
    profile_page = ReferenceField('ProfilePage', reverse_delete_rule=mongoengine.NULLIFY)

      

The declaration in this example means that when the Employee object is deleted, the ProfilePage that belongs to that employee is also deleted. If a whole batch of employees are removed, all associated profile pages are removed as well.

Used in code NULLIFY

, but explanation indicates use CASCADE

. Or am I missing something?

+3


source to share


1 answer


Deleting a user instance deletes his profile. This is how it works reverse_delete_rule=CASCADE

. Just like in relational databases.

You can 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 the documentation, now it's different:

class ProfilePage(Document):
    ...
    employee = ReferenceField('Employee', reverse_delete_rule=mongoengine.CASCADE)

      

The declaration in this example means that when the Employee object is deleted , the Profile that references that employee is also deleted . If an entire group of employees is deleted, all profile pages that are linked are also deleted.

+2


source







All Articles