1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use DbErr;
/// Turns `SeaORM`'s plain model `delete` into an error, so a row of a soft-deletable
/// entity leaves through [`soft_delete`](crate::SoftDeleteModel::soft_delete) or,
/// deliberately, [`force_delete`](crate::SoftDeleteModel::force_delete).
///
/// `SeaORM`'s `delete()` always removes the row, even on a soft-deleting entity,
/// so one call in the wrong place would erase data. Call this from the entity's
/// `before_delete` hook.
///
/// The hook runs only for `ActiveModel::delete` and `Model::delete`. Bulk deletes
/// (`Entity::delete_many()`, `Entity::delete_by_id(...)`) skip hooks in `SeaORM`, so
/// the guard cannot stop them; keep those out of code paths for this entity.
///
///
/// ```
/// # mod posts {
/// # use sea_orm::entity::prelude::*;
/// # #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
/// # #[sea_orm(table_name = "posts")]
/// # pub struct Model {
/// # #[sea_orm(primary_key)]
/// # pub id: i32,
/// # pub category_id: i32,
/// # pub slug: String,
/// # pub deleted_at: Option<DateTimeWithTimeZone>,
/// # }
/// # #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
/// # pub enum Relation {}
/// # }
/// # impl sea_orm_soft_delete::SoftDelete for posts::Entity {
/// # fn deleted_at_column() -> Self::Column { posts::Column::DeletedAt }
/// # }
/// use sea_orm::{ActiveModelBehavior, ConnectionTrait, DbErr};
///
/// #[async_trait::async_trait]
/// impl ActiveModelBehavior for posts::ActiveModel {
/// async fn before_delete<C: ConnectionTrait>(self, _db: &C) -> Result<Self, DbErr> {
/// sea_orm_soft_delete::deny_hard_delete()?;
/// Ok(self)
/// }
/// }
/// # fn main() {}
/// ```
///
/// # Errors
///
/// Always: that is its job.