feat: add soft delete mixin and session#1890
Conversation
- Add SoftDeleteMixin class with deleted_at field - Add SoftDeleteSession class that overrides delete method for soft deletes - Add tests for soft delete functionality - Fix datetime deprecation warning
d917490 to
5d0c23c
Compare
📝 Docs previewLast commit 8f42544 at: https://0e0527df.sqlmodel.pages.dev |
…lmodel into feat/soft-delete
d9e2b03 to
c43b622
Compare
…ve MRO issue - Change SoftDeleteMixin to not inherit from SQLModel (avoids MRO conflict) - Update all FastAPI tutorial files to use @asynccontextmanager instead of @contextmanager for lifespan (FastAPI 0.111+ requirement) - Add missing imports (HTTPException, Depends, Query) where needed - Fix test that called deprecated on_startup() method
…lmodel into feat/soft-delete
letsdeepchat
left a comment
There was a problem hiding this comment.
Summary
Adds soft delete support to SQLModel via two new classes:
SoftDeleteMixin— adds adeleted_at: Optional[datetime]column to any modelSoftDeleteSession— overridesdelete()to setdeleted_atinstead of removing the row. Acceptshard_delete=Trueto bypass and do a real delete.
Usage
from sqlmodel import SQLModel, Field, create_engine
from sqlmodel.soft_delete import SoftDeleteMixin
from sqlmodel.soft_delete_session import SoftDeleteSession as Session
class Hero(SoftDeleteMixin, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
with Session(engine) as session:
session.delete(hero) # soft delete — sets deleted_at
session.delete(hero, hard_delete=True) # real deleteChanges
sqlmodel/soft_delete.py—SoftDeleteMixinclasssqlmodel/soft_delete_session.py—SoftDeleteSessionclasstests/test_session_delete.py— 3 tests: soft delete, hard delete bypass, plain model unaffected
Notes
- Models not using
SoftDeleteMixinare completely unaffected - Works with both SQLAlchemy 1.4 and 2.x
- No changes to existing session or model internals
svlandeg
left a comment
There was a problem hiding this comment.
Could you have a look into the failing CI? I'll put this in draft in the meantime.
|
Heads-up: this will be closed in 3 days unless there's new activity. |
|
As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR. |
|
Hi @letsdeepchat , I’m currently learning SQLModel and was looking for soft delete support, then found this PR. From what I understand, this PR adds Would you still be interested in continuing this PR and adding query filtering support as well? Thanks! |
Hi @Sakura0122, Just my two cents as a passerby: this PR already represents a community contribution, and implementing automatic query filtering for soft-deleted rows is a fairly substantial feature on its own. If that behavior is important for your use case, it might be worth opening a separate issue or even contributing a dedicated PR, rather than expecting the original author to continue extending a closed PR. Thanks to everyone involved for improving SQLModel. |
Adds SoftDeleteMixin and SoftDeleteSession to support soft deletes with optional hard delete bypass.