Skip to main content

Model

Trait Model 

Source
pub trait Model<DB>:
    Sized
    + Send
    + Sync
    + Unpin
where DB: SqlDialect + Database, for<'r> Self: FromRow<'r, DB::Row>,
{
Show 28 methods // Required methods fn table_name() -> &'static str; fn create_table_sql() -> String; fn list_columns() -> Vec<String>; fn save<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send where E: IntoExecutor<'a, DB = DB>; fn update<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<UpdateResult, Error>> + Send where E: IntoExecutor<'a, DB = DB>; fn delete<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send where E: IntoExecutor<'a, DB = DB>; fn has_soft_delete() -> bool; fn find_by_id<'a, E>( executor: E, id: i32, ) -> impl Future<Output = Result<Option<Self>, Error>> + Send where E: IntoExecutor<'a, DB = DB>; // Provided methods fn save_fast<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn save_ultra<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn update_fast<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<UpdateResult, Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn update_ultra<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<UpdateResult, Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn delete_fast<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn delete_ultra<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn sensitive_fields() -> &'static [&'static str] { ... } fn relation_names() -> &'static [&'static str] { ... } fn default_includes() -> &'static [&'static str] { ... } fn from_row_fast(row: &DB::Row) -> Result<Self, Error> where usize: ColumnIndex<DB::Row>, for<'c> &'c str: ColumnIndex<DB::Row>, for<'r> Self: FromRow<'r, DB::Row> { ... } fn raw_sql<'q>( sql: &'q str, ) -> QueryAs<'q, DB, Self, <DB as Database>::Arguments<'q>> { ... } fn raw_sql_fast<'q>( sql: &'q str, ) -> QueryAs<'q, DB, FastRow<DB, Self>, <DB as Database>::Arguments<'q>> where Self: Sized, usize: ColumnIndex<DB::Row>, for<'c> &'c str: ColumnIndex<DB::Row> { ... } fn eager_load<'a>( _models: &mut [Self], _relation: &str, _executor: Executor<'a, DB>, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn find<'a, E>(executor: E) -> QueryBuilder<'a, Self, DB> where E: IntoExecutor<'a, DB = DB> { ... } fn all<'a, E>( executor: E, ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send where E: IntoExecutor<'a, DB = DB>, for<'q> <DB as Database>::Arguments<'q>: IntoArguments<'q, DB>, for<'c> &'c mut <DB as Database>::Connection: Executor<'c, Database = DB>, for<'c> &'c str: ColumnIndex<DB::Row>, DB::Connection: Send, Self: Send, String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB> { ... } fn find_one<'a, E>( executor: E, id: i32, ) -> impl Future<Output = Result<Option<Self>, Error>> + Send where E: IntoExecutor<'a, DB = DB> { ... } fn create<E>( executor: E, instance: Self, ) -> impl Future<Output = Result<Self, Error>> + Send where for<'e> E: IntoExecutor<'e, DB = DB> + Send { ... } fn update_by_id<'a, E>( executor: E, id: i32, json_patch: Value, ) -> impl Future<Output = Result<u64, Error>> + Send where E: IntoExecutor<'a, DB = DB>, for<'q> <DB as Database>::Arguments<'q>: IntoArguments<'q, DB>, for<'c> &'c mut <DB as Database>::Connection: Executor<'c, Database = DB>, for<'c> &'c str: ColumnIndex<DB::Row>, DB::Connection: Send, Self: Send, String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB> { ... } fn find_in_pool(pool: &Pool<DB>) -> QueryBuilder<'_, Self, DB> { ... } fn find_in_tx(conn: &mut DB::Connection) -> QueryBuilder<'_, Self, DB> { ... }
}
Expand description

The core trait for database models.

This trait provides the foundation for all database interactions for a specific entity. It is usually implemented automatically via #[derive(Model)].

Required Methods§

Source

fn table_name() -> &'static str

Returns the name of the database table associated with this model.

Source

fn create_table_sql() -> String

Returns the SQL string required to create the table for this model.

Source

fn list_columns() -> Vec<String>

Returns a list of column names for this model.

Source

fn save<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Saves the current instance to the database.

Source

fn update<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<UpdateResult, Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Updates the current instance in the database using optimistic locking if a version field exists.

Source

fn delete<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Deletes the current instance from the database (either hard or soft delete).

Source

fn has_soft_delete() -> bool

Returns whether this model supports soft deletes (via a deleted_at field).

Source

fn find_by_id<'a, E>( executor: E, id: i32, ) -> impl Future<Output = Result<Option<Self>, Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Finds a record by its Primary Key.

Provided Methods§

Source

fn save_fast<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Saves the current instance without hooks or extra safety checks.

Source

fn save_ultra<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Saves the current instance using the ultra-fast path (no hooks/extra checks). Note: On Postgres this may skip RETURNING and leave id unchanged.

Source

fn update_fast<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<UpdateResult, Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Updates the current instance without hooks or extra safety checks.

Source

fn update_ultra<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<UpdateResult, Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Updates the current instance using the ultra-fast path (no hooks/extra checks).

Source

fn delete_fast<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Deletes the current instance without hooks or extra safety checks.

Source

fn delete_ultra<'a, E>( &'a mut self, executor: E, ) -> impl Future<Output = Result<(), Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Deletes the current instance using the ultra-fast path (no hooks/extra checks).

Source

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

Returns a list of fields that are considered sensitive and should be redacted in logs.

Source

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

Returns the relation names available for eager loading.

Source

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

Returns relations that should be eager-loaded by default.

Source

fn from_row_fast(row: &DB::Row) -> Result<Self, Error>
where usize: ColumnIndex<DB::Row>, for<'c> &'c str: ColumnIndex<DB::Row>, for<'r> Self: FromRow<'r, DB::Row>,

Fast row mapping using positional indices (override in derives for speed).

Source

fn raw_sql<'q>( sql: &'q str, ) -> QueryAs<'q, DB, Self, <DB as Database>::Arguments<'q>>

Use raw SQL and map rows into the current model type.

Source

fn raw_sql_fast<'q>( sql: &'q str, ) -> QueryAs<'q, DB, FastRow<DB, Self>, <DB as Database>::Arguments<'q>>
where Self: Sized, usize: ColumnIndex<DB::Row>, for<'c> &'c str: ColumnIndex<DB::Row>,

Use raw SQL with fast positional mapping (select columns in model field order).

Source

fn eager_load<'a>( _models: &mut [Self], _relation: &str, _executor: Executor<'a, DB>, ) -> impl Future<Output = Result<(), Error>> + Send

Loads related models for a list of instances.

Source

fn find<'a, E>(executor: E) -> QueryBuilder<'a, Self, DB>
where E: IntoExecutor<'a, DB = DB>,

Creates a new QueryBuilder for this model.

Source

fn all<'a, E>( executor: E, ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send
where E: IntoExecutor<'a, DB = DB>, for<'q> <DB as Database>::Arguments<'q>: IntoArguments<'q, DB>, for<'c> &'c mut <DB as Database>::Connection: Executor<'c, Database = DB>, for<'c> &'c str: ColumnIndex<DB::Row>, DB::Connection: Send, Self: Send, String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB>,

Fetches all records for this model.

Source

fn find_one<'a, E>( executor: E, id: i32, ) -> impl Future<Output = Result<Option<Self>, Error>> + Send
where E: IntoExecutor<'a, DB = DB>,

Finds a record by its primary key (alias for [find_by_id]).

Source

fn create<E>( executor: E, instance: Self, ) -> impl Future<Output = Result<Self, Error>> + Send
where for<'e> E: IntoExecutor<'e, DB = DB> + Send,

Creates a new record and returns the saved instance.

Source

fn update_by_id<'a, E>( executor: E, id: i32, json_patch: Value, ) -> impl Future<Output = Result<u64, Error>> + Send
where E: IntoExecutor<'a, DB = DB>, for<'q> <DB as Database>::Arguments<'q>: IntoArguments<'q, DB>, for<'c> &'c mut <DB as Database>::Connection: Executor<'c, Database = DB>, for<'c> &'c str: ColumnIndex<DB::Row>, DB::Connection: Send, Self: Send, String: for<'q> Encode<'q, DB> + Type<DB>, i64: for<'q> Encode<'q, DB> + Type<DB>, f64: for<'q> Encode<'q, DB> + Type<DB>, bool: for<'q> Encode<'q, DB> + Type<DB>, Option<String>: for<'q> Encode<'q, DB> + Type<DB>, Uuid: for<'q> Encode<'q, DB> + Type<DB>, DateTime<Utc>: for<'q> Encode<'q, DB> + Type<DB>, NaiveDateTime: for<'q> Encode<'q, DB> + Type<DB>, NaiveDate: for<'q> Encode<'q, DB> + Type<DB>, Json<Value>: for<'q> Encode<'q, DB> + Type<DB>,

Applies a JSON patch update by primary key.

Source

fn find_in_pool(pool: &Pool<DB>) -> QueryBuilder<'_, Self, DB>

Creates a new QueryBuilder using a connection pool.

Source

fn find_in_tx(conn: &mut DB::Connection) -> QueryBuilder<'_, Self, DB>

Creates a new QueryBuilder using an active database connection.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§