Commit 66457b81 authored by Yury's avatar Yury

document: enable cascade update and delete

parent 692090af
Pipeline #331 passed with stages
in 4 minutes and 12 seconds
......@@ -212,9 +212,10 @@ class Document(metaclass=MetaDocument):
for self_field, other_field in related_fields:
if self_field.parent:
await other_field.document_cls.collection.update_many({
other_field.name: self.shadow_copy[self_field.name]
}, {"$set": {other_field.name: self[self_field.name]}})
for document in await other_field.document_cls.many({
other_field.name: self.shadow_copy[self_field.name]}):
document[other_field.name] = self[self_field.name]
await document.push_update()
@check_setup
async def push_update(self):
......@@ -269,9 +270,9 @@ class Document(metaclass=MetaDocument):
for self_field, other_field in related_fields:
if self_field.parent:
await other_field.document_cls.collection.delete_many({
other_field.name: self[self_field.name]
})
for document in await other_field.document_cls.many(**{
other_field.name: self[self_field.name]}):
await document.delete()
async def delete(self):
"""Delete current document and related fields or documents base on related."""
......
......@@ -230,7 +230,7 @@ async def test_related(client):
Like = mock_Document("likes", client, **{
"video": Document.related(Video.Field._id, multiple=False, parent=False,
other_is_parent=True)
other_is_parent=False)
})
video = await Video.create(author="me")
......@@ -249,7 +249,7 @@ async def test_related(client):
assert await client["mdocument"]["videos"].find().to_list(9999) == []
assert await client["mdocument"]["comments"].find().to_list(9999) == []
assert await client["mdocument"]["likes"].find().to_list(9999) == []
assert await client["mdocument"]["likes"].find().to_list(9999) == [like]
def test___eq__():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment