diff --git a/peewee_async.py b/peewee_async.py index bfbc1b7..898c9f1 100644 --- a/peewee_async.py +++ b/peewee_async.py @@ -689,6 +689,11 @@ async def fetch_results(self, cursor): return await self._database.last_insert_id_async(cursor) +class AioModelRaw(peewee.ModelRaw, AioQueryMixin): + async def fetch_results(self, cursor): + return await self.make_async_query_wrapper(cursor) + + class AioModelSelect(peewee.ModelSelect, AioQueryMixin): async def fetch_results(self, cursor): @@ -764,6 +769,10 @@ def insert_from(cls, query, fields): else field for field in fields] return AioModelInsert(cls, insert=query, columns=columns) + @classmethod + def raw(cls, sql, *params): + return AioModelRaw(cls, sql, params) + @classmethod def delete(cls): return AioModelDelete(cls) diff --git a/tests/aio_model/test_selecting.py b/tests/aio_model/test_selecting.py index 29bc7bb..be69191 100644 --- a/tests/aio_model/test_selecting.py +++ b/tests/aio_model/test_selecting.py @@ -1,4 +1,6 @@ import peewee + +from peewee_async import AioModelRaw from tests.conftest import manager_for_all_dbs, dbs_all from tests.models import TestModel, TestModelAlpha, TestModelBeta @@ -17,6 +19,18 @@ async def test_select_w_join(db): assert result.joined_alpha.id == alpha.id +@dbs_all +async def test_raw_select(db): + obj1 = await TestModel.aio_create(text="Test 1") + obj2 = await TestModel.aio_create(text="Test 2") + query = TestModel.raw( + 'SELECT id, text, data FROM testmodel m ORDER BY m.text' + ) + assert isinstance(query, AioModelRaw) + result = await query.aio_execute() + assert list(result) == [obj1, obj2] + + @manager_for_all_dbs async def test_select_compound(manager): obj1 = await manager.create(TestModel, text="Test 1")