You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Best practice for common filters like for soft-delete when using the asyncio API #7973
I have entities, which are supposed to be soft-deleted by setting an deleted_at attribute per entity. If that value is greater than timestamptz('1970-01-01T00:00:00+00:00'), the instance / row is considered deleted. My goal is, to let entites / rows marked as soft-deleted to completely disappear for the whole application.
For the query API of SqlAlchemy, it was easy to establish a custom Query subclass, wire that properly and required filters, to omit soft-deleted items in all operations.
When using the asyncio API, the Query class is not used, but sqlalchemy/sql/expression.py#select statements are created. From what I understand, it is not easily possible to wire in some extra filter expression, which is automagically in place.
Databases / Backends / Drivers targeted
Postgresql
Example Use
Lets assume I'm creating a statement to gather multiple entities:
without having the requirement to express this filter part: filter(deleted_at == POSIX_ZERO) in the statement. I'm looking for a way to have this filter part present in all statements, automatically. This example is focussed on reading, select, but my goal is to apply this to all CRUD operations equally.
Activity
zzzeek commentedon Apr 28, 2022
the soft delete pattern in SQLAlchemy 1.4 is now provided by the do_orm_execute() event in conjunction with the with_loader_criteria() ORM option.
Docs and examples are at the following links:
https://docs.sqlalchemy.org/en/14/orm/session_events.html#session-execute-events
https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.with_loader_criteria
https://docs.sqlalchemy.org/en/14/_modules/examples/extending_query/filter_public.html
pansen commentedon Apr 28, 2022
Awesome, adding global where criteria is what I was looking for.
Thank you very much!
fix(soft-delete): two bugs flagged in codeant review
fix(soft-delete): two bugs flagged in codeant review
fix(soft-delete): two bugs flagged in codeant review
fix(soft-delete): two bugs flagged in codeant review
fix(soft-delete): two bugs flagged in codeant review
fix(soft-delete): two bugs flagged in codeant review