Skip to main content

Model

Derive Macro Model 

Source
#[derive(Model)]
{
    // Attributes available to this derive:
    #[table]
    #[column]
    #[primary_key]
    #[timestamps]
    #[soft_delete]
    #[version]
}
Available on crate feature macros and (crate features model-mysql or model-postgres or model-sqlite) only.
Expand description

Derive rust_web_server::model::Model for a struct.

§Struct attributes

AttributeMeaning
#[table(name = "tbl")]Override table name (default: struct name lowercased)
#[timestamps]save sets the created_at and updated_at fields’ columns (17.31.0)
#[soft_delete]delete sets the deleted_at field’s column (an Option); queries skip such rows (17.31.0)

§Field attributes

AttributeMeaning
#[primary_key]Mark as primary key (i64, i32, String or Uuid)
#[primary_key(auto_increment)]Auto-increment primary key
#[column(name = "col")]Override column name
#[column(unique)]Mark column unique (informational)
#[version]Optimistic-locking version (an integer), checked and incremented by save (17.31.0)
#[ignore]Exclude from DB mapping

T::repository(&pool) is keyed by i64 for an integer primary key of any width (as before 17.31.0) and by the field’s type otherwise (String, Uuid).

§Example

ⓘ
#[derive(rust_web_server::Model, Debug, Clone)]
#[table(name = "users")]
pub struct User {
    #[primary_key(auto_increment)]
    pub id: i64,
    #[column(name = "first_name")]
    pub name: String,
    pub email: String,
    #[ignore]
    pub display_label: String,
}

// Conventions the repository maintains (17.31.0):
#[derive(rust_web_server::Model, Debug, Clone)]
#[table(name = "posts")]
#[timestamps]
#[soft_delete]
pub struct Post {
    #[primary_key]
    pub id: rust_web_server::model::Uuid, // Uuid::NIL → save picks a v7
    pub title: String,
    #[version]
    pub lock_version: i64,
    pub created_at: rust_web_server::model::Timestamp,
    pub updated_at: rust_web_server::model::Timestamp,
    pub deleted_at: Option<rust_web_server::model::Timestamp>,
}