Skip to content

Extend DeriveIntoActiveModel to support settable and default-able values #2960

@sinder38

Description

@sinder38

Motivation

When using DeriveIntoActiveModel for my create and patch API/Serde endpoint input structs, there is no way to express:

  1. “If the user didn’t provide this field, use my custom default.”
  2. “Always use this set value for this required field.”

Currently, this requires either a manual implementation or explicitly setting those values every time the DeriveIntoActiveModel struct is used.

Example model that would benefit from this feature:

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "public", table_name = "file")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: I32,
    pub creator_id: i32,
    pub created_at: DateTime<Utc>, // <- set using current_time function
    pub updater_id: Option<i32>,
    pub updated_at: Option<DateTime<Utc>>,
    pub deleted: bool, // <- set using my own default every time
}

Proposed Solutions

I will link my PR below with more details.

Additional Information

I also believe the documentation for this derive macro is lacking.

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