Skip to content

Commit

Permalink
Add validation for non-existing items to update method on CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
julianaklulo committed Oct 21, 2024
1 parent 9ae2402 commit 2bd5062
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,7 @@ async def update(
Raises:
MultipleResultsFound: If `allow_multiple` is `False` and more than one record matches the filters.
NoResultFound: If no record matches the filters.
ValueError: If extra fields not present in the model are provided in the update data.
ValueError: If `return_as_model` is `True` but `schema_to_select` is not provided.
Expand Down Expand Up @@ -2199,7 +2200,10 @@ async def update(
)
```
"""
if not allow_multiple and (total_count := await self.count(db, **kwargs)) > 1:
total_count = await self.count(db, **kwargs)
if total_count == 0:
raise NoResultFound("No record found to update.")
if not allow_multiple and total_count > 1:
raise MultipleResultsFound(
f"Expected exactly one record to update, found {total_count}."
)
Expand Down

0 comments on commit 2bd5062

Please sign in to comment.