Skip to main content

Crate sea_orm_soft_delete

Crate sea_orm_soft_delete 

Source
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§

SoftDelete
Marks an entity as soft-deletable and adds table-wide trash operations.
SoftDeleteModel
Trash operations on one row. Every Model of a SoftDelete entity gets them.
TrashedQuery
Trash filters for any query on a SoftDelete entity.

Functions§

deleted_at_column
The deleted_at column for a migration: nullable timestamp with time zone.
deny_hard_delete
Turns SeaORM’s plain model delete into an error, so a row of a soft-deletable entity leaves through soft_delete or, deliberately, force_delete.
unique_while_kept
A unique index that only covers rows outside the trash: CREATE UNIQUE INDEX ... WHERE deleted_at IS NULL.