Deleting Models
10/30/24Less than 1 minute
Deleting Models
Hard Delete
Permanently remove a record:
// Delete by ID
entity::sys_user::Entity::delete_by_id(1001).exec(&db).await?;
// Delete with conditions
use sea_orm::{EntityTrait, QueryFilter, ColumnTrait};
entity::sys_user::Entity::delete_many()
.filter(entity::sys_user::Column::Status.eq(0))
.exec(&db).await?;Soft Delete
In QiLuo, soft delete is preferred. Mark a record as deleted instead of removing it:
let mut user: entity::sys_user::ActiveModel = entity::sys_user::Entity::find_by_id(1001)
.one(&db)
.await?
.ok_or_else(|| anyhow::anyhow!("not found"))?
.into();
user.deleted_at = Set(Some(chrono::Utc::now()));
user.update(&db).await?;For queries, always filter out soft-deleted records:
entity::sys_user::Entity::find()
.filter(entity::sys_user::Column::DeletedAt.is_null())
.all(&db).await?;