Expand description
Soft deletes for SeaORM: a row is moved to the
trash by setting deleted_at, can be restored, and is pruned for good later.
use sea_orm::{DatabaseConnection, DbErr, EntityTrait};
use sea_orm_soft_delete::{SoftDelete, SoftDeleteModel};
impl SoftDelete for posts::Entity {
fn deleted_at_column() -> Self::Column {
posts::Column::DeletedAt
}
}
async fn example(db: &DatabaseConnection, post: posts::Model) -> Result<(), DbErr> {
let post = post.soft_delete(db).await?; // deleted_at = now
let live = posts::Entity::find_without_trashed().all(db).await?;
let post = post.restore(db).await?; // deleted_at = NULL
Ok(())
}SeaORM has no global query scopes, so Entity::find() still returns trashed
rows. Use SoftDelete::find_without_trashed or
TrashedQuery::without_trashed wherever trashed rows must stay hidden.
Traits§
- Soft
Delete - Marks an entity as soft-deletable and adds table-wide trash operations.
- Soft
Delete Model - Trash operations on one row. Every
Modelof aSoftDeleteentity gets them. - Trashed
Query - Trash filters for any query on a
SoftDeleteentity.
Functions§
- deleted_
at_ column - The
deleted_atcolumn for a migration: nullabletimestamp with time zone. - deny_
hard_ delete - Turns
SeaORM’s plain modeldeleteinto an error, so a row of a soft-deletable entity leaves throughsoft_deleteor, deliberately,force_delete. - unique_
while_ kept - A unique index that only covers rows outside the trash:
CREATE UNIQUE INDEX ... WHERE deleted_at IS NULL.