Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync {
    // Required methods
    fn table_name() -> &'static str;
    fn column_names() -> &'static [&'static str];
    fn primary_key_name() -> &'static str;
    fn primary_key_value(&self) -> Value;
    fn from_row(row: &ModelRow) -> Result<Self, DbError>;
    fn to_values(&self) -> Vec<(&'static str, Value)>;

    // Provided methods
    fn primary_key_auto_increment() -> bool { ... }
    fn created_at_column() -> Option<&'static str> { ... }
    fn updated_at_column() -> Option<&'static str> { ... }
    fn soft_delete_column() -> Option<&'static str> { ... }
    fn version_column() -> Option<&'static str> { ... }
}
Available on crate features model-mysql or model-postgres or model-sqlite only.
Expand description

Trait generated by #[derive(Model)]. Maps a Rust struct to a database table.

Required Methods§

Source

fn table_name() -> &'static str

The database table name.

Source

fn column_names() -> &'static [&'static str]

All column names (excluding #[ignore] fields).

Source

fn primary_key_name() -> &'static str

The primary key column name.

Source

fn primary_key_value(&self) -> Value

The primary key value for this instance.

Source

fn from_row(row: &ModelRow) -> Result<Self, DbError>

Deserialise a database row into this type.

Source

fn to_values(&self) -> Vec<(&'static str, Value)>

Serialise this instance’s fields as (column, value) pairs. Excludes #[ignore] fields.

Provided Methods§

Source

fn primary_key_auto_increment() -> bool

Whether the primary key is auto-incremented by the database.

Source

fn created_at_column() -> Option<&'static str>

The column save sets to the time of the insert and never writes again (#[timestamps]: created_at). None: no such column.

Source

fn updated_at_column() -> Option<&'static str>

The column save sets to the time of every insert and update (#[timestamps]: updated_at).

Source

fn soft_delete_column() -> Option<&'static str>

The soft-delete column (#[soft_delete]: deleted_at): delete sets it to the time instead of removing the row, and queries leave out rows where it isn’t NULL unless asked for them.

Source

fn version_column() -> Option<&'static str>

The optimistic-locking column (#[version]): 1 on insert, one more on every update, which only succeeds while the row still has the version this copy was read with.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§