Skip to content

Commit

Permalink
Merge pull request #51 from JakNowy/optimize-count
Browse files Browse the repository at this point in the history
Skip count call when possible
  • Loading branch information
igorbenav authored Apr 14, 2024
2 parents 19e038a + 3fb3c2e commit 31a2066
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,8 +1358,7 @@ async def update(
update(db, {'username': 'new_username'}, id__ne=1, allow_multiple=False)
```
"""
total_count = await self.count(db, **kwargs)
if not allow_multiple and total_count > 1:
if not allow_multiple and (total_count := await self.count(db, **kwargs)) > 1:
raise MultipleResultsFound(
f"Expected exactly one record to update, found {total_count}."
)
Expand Down Expand Up @@ -1423,8 +1422,7 @@ async def db_delete(
db_delete(db, username='unique_username', allow_multiple=False)
```
"""
total_count = await self.count(db, **kwargs)
if not allow_multiple and total_count > 1:
if not allow_multiple and (total_count := await self.count(db, **kwargs)) > 1:
raise MultipleResultsFound(
f"Expected exactly one record to delete, found {total_count}."
)
Expand Down

0 comments on commit 31a2066

Please sign in to comment.