Mongoengine: reverse_delete_rule PULL not working on self reference fields

Created on 27 Mar 2017  路  7Comments  路  Source: MongoEngine/mongoengine

Using the following model:

class Record(db.Document):
    children = db.ListField(db.ReferenceField('self'), reverse_delete_rule = 'PULL')

and appending a child so that print of record.children looks like:
[<Record: my record>]

and then deleting the child results in print of parent record.children becoming:
[DBRef('record', ObjectId('58d98058b6697b29544fef21'))]

It seems that the child is not being pulled from the parent upon deletion....???

Bug

Most helpful comment

 def test_reverse_delete_rule_pull(self):
        """Ensure that a referenced document is also deleted with
        pull.
        """
        class Record(Document):
            name = StringField()
            children = ListField(ReferenceField('self', reverse_delete_rule=PULL))

        Record.drop_collection()

        parent_record = Record(name='parent').save()
        child_record = Record(name='child').save()
        parent_record.children.append(child_record)
        parent_record.save()

        child_record.delete()
        self.assertEqual(Record.objects(name='parent').get().children, [])

@wojcikstefan here is the test case, it passing.

All 7 comments

Hi @spitzbubchen, thank you for reporting this! Could you submit a PR with a failing test case?

 def test_reverse_delete_rule_pull(self):
        """Ensure that a referenced document is also deleted with
        pull.
        """
        class Record(Document):
            name = StringField()
            children = ListField(ReferenceField('self', reverse_delete_rule=PULL))

        Record.drop_collection()

        parent_record = Record(name='parent').save()
        child_record = Record(name='child').save()
        parent_record.children.append(child_record)
        parent_record.save()

        child_record.delete()
        self.assertEqual(Record.objects(name='parent').get().children, [])

@wojcikstefan here is the test case, it passing.

If I use this expression without reloading instance, it will fail obviously.

 self.assertEqual(parent_record.children, [])

is it still bug or what?

if it is still considered a bug, i am ready to submit PR :)

After I reloading instance, it seems reverse_delete_rule with pull worked. I think it is not a bug.

@wojcikstefan any thoughts?

Added passing test case and closing this issue

Was this page helpful?
0 / 5 - 0 ratings