Skip to content

Custom ID, Soft Delete, Optimistic Lock & Auto Field Fill #2969

@tttq

Description

@tttq

Dear Sea-orm developers, hello. I have the following requests:

I would like to request support for the following features in SeaORM, with the ability to customize related fields and implement them via macro annotations:

  1. Custom ID Generation
  • Support custom primary key generation rules (e.g., distributed ID).

  • Auto-populate the primary key when inserting a new record.

  • Allow customization of the primary key field name (not limited to fixed names like id).

  1. Soft Delete
  • Default value 0 for the soft delete field on insertion; set to 1 instead of performing hard delete.

  • Automatically add soft delete condition (e.g., is_deleted = 0) to all queries by default.

  • Allow customization of the soft delete field name (e.g., deleted, is_remove instead of is_deleted).

  1. Optimistic Locking
  • Default version value 0 on insertion with validation; auto increment version by 1 on update and soft delete.

  • Allow customization of the version field name (e.g., lock_version instead of version).

  1. Auto Field Fill & Update
  • On insert: Auto populate created_at, created_by, set soft delete field to default 0, version field to default 0.

  • On update: Auto increment version, populate updated_at, updated_by.

  • On soft delete: Set soft delete field to 1, increment version, populate updated_at, updated_by.

  • Allow full customization of all auto-filled fields (e.g., create_time instead of created_at, operator instead of created_by).

Preferred Implementation Approach

These features are expected to be implemented through macro annotations (attributes) on entity fields, for example:

    • #[sea_orm(primary_key, generator = "distributed_id")] for custom ID generation
    • #[sea_orm(soft_delete, default = 0, deleted_value = 1)] for soft delete field
    • #[sea_orm(optimistic_lock, default = 0)] for version field
    • #[sea_orm(auto_fill = "created_at")] / #[sea_orm(auto_fill = "updated_by")] for auto-filled fields

Use Case

These features are widely used in enterprise applications and can greatly reduce boilerplate code for data integrity, traceability, and concurrency control. Customizable fields and macro-based implementation will make the features more flexible and adaptable to different project specifications.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions