SeaQL / SeaQL/sea-orm

[Feature Request] Soft Delete

Open
#220 18 comments 3 reactions 1 assignee View on GitHub

@billy1624 is already working on this.

Since Oct 28, 2021.

Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Use case
  • Sometimes we can't always perform hard deletions in production.
  • This is what most ORMs do
The Ideal way
  • Respect the deleted_at column (pub deleted_at: Option<DateTimeWithTimeZone>)
  • All deleted_at checkings should run behind the scenes instead manually adding AND deleted_at IS NULL
  • deleted_at columns should be indexed for performance reasons

Delete

let res: DeleteResult = fruit.delete(db).await?; // ID = 10

Should generate the SQL below

-- Soft Delete - If the column deleted_at exists
UPDATE `fruit` SET `deleted_at`="2020-10-29 10:23" WHERE id = 10

-- Hard Delete - If the column deleted_at doesn't exist
DELETE FROM `fruit` WHERE `fruit`.`id` = 10'

Select

let cheese: Option<cake::Model> = Cake::find_by_id(1).one(db).await?;

Should generate the SQL below

-- If the column deleted_at exists
SELECT * FROM cake WHERE id = 1 AND deleted_at IS NULL

-- If the column deleted_at doesn't exist
SELECT * FROM cake WHERE id = 1

Golang GORM

PS: I am a Go developer most of the time.

GORM, the popular ORM for Go, has defined a default struct called gorm.Model.

// gorm.Model definition
type Model struct {
  ID        uint           `gorm:"primaryKey"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt gorm.DeletedAt `gorm:"index"`
}

And any struct with gorm.Model in it will automatically become soft-delete aware.

type User struct {
  gorm.Model
  Name       string
  Phone      string
}

If one really needs to perform hard deletion, here's how in GORM:

db.Delete(&order)
// UPDATE orders SET deleted_at="2020-10-29 10:23" WHERE id = 10;

db.Unscoped().Delete(&order)
// DELETE FROM orders WHERE id=10;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.