Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phenobarbital committed Oct 19, 2023
1 parent 5c27ac6 commit 2b16065
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions asyncdb/drivers/pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,7 @@ async def _insert_(self, _model: Model, **kwargs): # pylint: disable=W0613
message=f"Error on Insert over table {_model.Meta.name}: {err!s}"
) from err

async def _delete_(self, _model: Model, **kwargs): # pylint: disable=W0613
async def _delete_(self, _model: Model, _filter: dict = None, **kwargs): # pylint: disable=W0613
"""
delete a row from model.
"""
Expand All @@ -1345,7 +1345,8 @@ async def _delete_(self, _model: Model, **kwargs): # pylint: disable=W0613
except AttributeError:
table = _model.__name__
source = []
_filter = {}
if not _filter:
_filter = {}
n = 1
fields = _model.columns()
for _, field in fields.items():
Expand All @@ -1361,9 +1362,16 @@ async def _delete_(self, _model: Model, **kwargs): # pylint: disable=W0613
)
n += 1
if pk := self._get_attribute(field, value, attr='primary_key'):
if column in _filter:
# already this value on delete:
continue
_filter[column] = pk
try:
condition = self._where(fields, **_filter)
if not condition:
raise DriverError(
f"Avoid DELETE without WHERE conditions: {_filter}"
)
_delete = f"DELETE FROM {table} {condition};"
self._logger.debug(f'DELETE: {_delete}')
result = await self._connection.execute(_delete)
Expand Down
3 changes: 2 additions & 1 deletion asyncdb/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async def update(self, **kwargs):
f"Error on UPDATE {self.Meta.name}: {err}"
) from err

async def delete(self, **kwargs):
async def delete(self, _filter: dict = None, **kwargs):
"""
Deleting a row Model based on Primary Key
"""
Expand All @@ -169,6 +169,7 @@ async def delete(self, **kwargs):
try:
result = await self.Meta.connection._delete_(
_model=self,
_filter=_filter,
**kwargs
)
return result
Expand Down
2 changes: 1 addition & 1 deletion asyncdb/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__title__ = 'asyncdb'
__description__ = ('Library for Asynchronous data source connections '
'Collection of asyncio drivers.')
__version__ = '2.5.2'
__version__ = '2.5.3'
__author__ = 'Jesus Lara'
__author_email__ = '[email protected]'
__license__ = 'BSD'

0 comments on commit 2b16065

Please sign in to comment.