Skip to content

Commit c7707f3

Browse files
committed
feat: added aio_delete_instance method
1 parent 3089e5f commit c7707f3

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

peewee_async.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,18 @@ def raw(cls, sql, *params):
798798
def delete(cls):
799799
return AioModelDelete(cls)
800800

801+
async def aio_delete_instance(self, recursive=False, delete_nullable=False):
802+
if recursive:
803+
dependencies = self.dependencies(delete_nullable)
804+
for query, fk in reversed(list(dependencies)):
805+
print(query, fk)
806+
model = fk.model
807+
if fk.null and not delete_nullable:
808+
await model.update(**{fk.name: None}).where(query).aio_execute()
809+
else:
810+
await model.delete().where(query).aio_execute()
811+
return await type(self).delete().where(self._pk_expr()).aio_execute()
812+
801813
@classmethod
802814
async def aio_get(cls, *query, **filters):
803815
"""Async version of **peewee.Model.get**"""

peewee_async_compat.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ async def update(self, obj, only=None):
316316

317317
async def delete(self, obj, recursive=False, delete_nullable=False):
318318
"""Delete object from database."""
319+
warnings.warn(
320+
"`delete` method is deprecated, use `AioModel.aio_delete_instance` instead.",
321+
DeprecationWarning
322+
)
319323
if recursive:
320324
dependencies = obj.dependencies(delete_nullable)
321325
for cond, fk in reversed(list(dependencies)):

tests/aio_model/test_shortcuts.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import uuid
2+
13
import pytest
24
from peewee import fn
35

46
from tests.conftest import dbs_all
5-
from tests.models import TestModel, IntegerTestModel
7+
from tests.models import TestModel, IntegerTestModel, TestModelAlpha, TestModelBeta
68

79

810
@dbs_all
@@ -61,3 +63,26 @@ async def test_count_query_clear_limit(db):
6163
await IntegerTestModel.aio_create(num=num)
6264
count = await IntegerTestModel.select().limit(3).aio_count(clear_limit=True)
6365
assert count == 5
66+
67+
68+
@dbs_all
69+
async def test_aio_delete_instance(db):
70+
text = "Test %s" % uuid.uuid4()
71+
obj1 = await TestModel.aio_create(text=text)
72+
obj2 = await TestModel.aio_get(id=obj1.id)
73+
74+
await obj2.aio_delete_instance()
75+
76+
obj3 = await TestModel.aio_get_or_none(id=obj1.id)
77+
assert obj3 is None
78+
79+
80+
@dbs_all
81+
async def test_aio_delete_instance_with_fk(db):
82+
alpha = await TestModelAlpha.aio_create(text="test")
83+
beta = await TestModelBeta.aio_create(alpha=alpha, text="test")
84+
85+
await alpha.aio_delete_instance(recursive=True)
86+
87+
assert await TestModelAlpha.aio_get_or_none(id=alpha.id) is None
88+
assert await TestModelBeta.aio_get_or_none(id=beta.id) is None

tests/compat/test_shortcuts.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ async def test_create_obj(manager):
6969
obj = await manager.create(CompatTestModel, text=text)
7070
assert obj is not None
7171
assert obj.text == text
72+
73+
74+
@manager_for_all_dbs
75+
async def test_delete_obj(manager):
76+
text = "Test %s" % uuid.uuid4()
77+
obj1 = await manager.create(CompatTestModel, text=text)
78+
obj2 = await manager.get(CompatTestModel, id=obj1.id)
79+
80+
await manager.delete(obj2)
81+
82+
obj3 = await manager.get_or_none(CompatTestModel, id=obj1.id)
83+
assert obj3 is None

tests/test_shortcuts.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,6 @@ async def test_prefetch(manager, prefetch_type):
4343
assert tuple(result[0].betas[0].gammas) == (gamma_111, gamma_112)
4444

4545

46-
@manager_for_all_dbs
47-
async def test_delete_obj(manager):
48-
text = "Test %s" % uuid.uuid4()
49-
obj1 = await manager.create(TestModel, text=text)
50-
obj2 = await manager.get(TestModel, id=obj1.id)
51-
52-
await manager.delete(obj2)
53-
54-
obj3 = await manager.get_or_none(TestModel, id=obj1.id)
55-
assert obj3 is None
56-
57-
5846
@manager_for_all_dbs
5947
async def test_update_obj(manager):
6048

0 commit comments

Comments
 (0)