diff --git a/peewee_async.py b/peewee_async.py index b3cb385..7b1f950 100644 --- a/peewee_async.py +++ b/peewee_async.py @@ -798,6 +798,18 @@ def raw(cls, sql, *params): def delete(cls): return AioModelDelete(cls) + async def aio_delete_instance(self, recursive=False, delete_nullable=False): + if recursive: + dependencies = self.dependencies(delete_nullable) + for query, fk in reversed(list(dependencies)): + print(query, fk) + model = fk.model + if fk.null and not delete_nullable: + await model.update(**{fk.name: None}).where(query).aio_execute() + else: + await model.delete().where(query).aio_execute() + return await type(self).delete().where(self._pk_expr()).aio_execute() + @classmethod async def aio_get(cls, *query, **filters): """Async version of **peewee.Model.get**""" diff --git a/peewee_async_compat.py b/peewee_async_compat.py index c1821af..adaa143 100644 --- a/peewee_async_compat.py +++ b/peewee_async_compat.py @@ -316,6 +316,10 @@ async def update(self, obj, only=None): async def delete(self, obj, recursive=False, delete_nullable=False): """Delete object from database.""" + warnings.warn( + "`delete` method is deprecated, use `AioModel.aio_delete_instance` instead.", + DeprecationWarning + ) if recursive: dependencies = obj.dependencies(delete_nullable) for cond, fk in reversed(list(dependencies)): diff --git a/tests/aio_model/test_shortcuts.py b/tests/aio_model/test_shortcuts.py index c0fcc4e..8c86b78 100644 --- a/tests/aio_model/test_shortcuts.py +++ b/tests/aio_model/test_shortcuts.py @@ -1,8 +1,10 @@ +import uuid + import pytest from peewee import fn from tests.conftest import dbs_all -from tests.models import TestModel, IntegerTestModel +from tests.models import TestModel, IntegerTestModel, TestModelAlpha, TestModelBeta @dbs_all @@ -61,3 +63,26 @@ async def test_count_query_clear_limit(db): await IntegerTestModel.aio_create(num=num) count = await IntegerTestModel.select().limit(3).aio_count(clear_limit=True) assert count == 5 + + +@dbs_all +async def test_aio_delete_instance(db): + text = "Test %s" % uuid.uuid4() + obj1 = await TestModel.aio_create(text=text) + obj2 = await TestModel.aio_get(id=obj1.id) + + await obj2.aio_delete_instance() + + obj3 = await TestModel.aio_get_or_none(id=obj1.id) + assert obj3 is None + + +@dbs_all +async def test_aio_delete_instance_with_fk(db): + alpha = await TestModelAlpha.aio_create(text="test") + beta = await TestModelBeta.aio_create(alpha=alpha, text="test") + + await alpha.aio_delete_instance(recursive=True) + + assert await TestModelAlpha.aio_get_or_none(id=alpha.id) is None + assert await TestModelBeta.aio_get_or_none(id=beta.id) is None diff --git a/tests/compat/test_shortcuts.py b/tests/compat/test_shortcuts.py index a27717f..8157d60 100644 --- a/tests/compat/test_shortcuts.py +++ b/tests/compat/test_shortcuts.py @@ -69,3 +69,15 @@ async def test_create_obj(manager): obj = await manager.create(CompatTestModel, text=text) assert obj is not None assert obj.text == text + + +@manager_for_all_dbs +async def test_delete_obj(manager): + text = "Test %s" % uuid.uuid4() + obj1 = await manager.create(CompatTestModel, text=text) + obj2 = await manager.get(CompatTestModel, id=obj1.id) + + await manager.delete(obj2) + + obj3 = await manager.get_or_none(CompatTestModel, id=obj1.id) + assert obj3 is None diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py index 562b21e..48ff8a7 100644 --- a/tests/test_shortcuts.py +++ b/tests/test_shortcuts.py @@ -43,18 +43,6 @@ async def test_prefetch(manager, prefetch_type): assert tuple(result[0].betas[0].gammas) == (gamma_111, gamma_112) -@manager_for_all_dbs -async def test_delete_obj(manager): - text = "Test %s" % uuid.uuid4() - obj1 = await manager.create(TestModel, text=text) - obj2 = await manager.get(TestModel, id=obj1.id) - - await manager.delete(obj2) - - obj3 = await manager.get_or_none(TestModel, id=obj1.id) - assert obj3 is None - - @manager_for_all_dbs async def test_update_obj(manager):