pub trait Model<DB>:
Sized
+ Send
+ Sync
+ Unpin{
Show 22 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 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 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§
Sourcefn table_name() -> &'static str
fn table_name() -> &'static str
Returns the name of the database table associated with this model.
Sourcefn create_table_sql() -> String
fn create_table_sql() -> String
Returns the SQL string required to create the table for this model.
Sourcefn list_columns() -> Vec<String>
fn list_columns() -> Vec<String>
Returns a list of column names for this model.
Sourcefn save<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn save<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Saves the current instance to the database.
Sourcefn update<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<UpdateResult, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn update<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<UpdateResult, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Updates the current instance in the database using optimistic locking if a version field exists.
Sourcefn delete<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn delete<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Deletes the current instance from the database (either hard or soft delete).
Sourcefn has_soft_delete() -> bool
fn has_soft_delete() -> bool
Returns whether this model supports soft deletes (via a deleted_at field).
Sourcefn find_by_id<'a, E>(
executor: E,
id: i32,
) -> impl Future<Output = Result<Option<Self>, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn find_by_id<'a, E>(
executor: E,
id: i32,
) -> impl Future<Output = Result<Option<Self>, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Finds a record by its Primary Key.
Provided Methods§
Sourcefn save_fast<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn save_fast<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Saves the current instance without hooks or extra safety checks.
Sourcefn save_ultra<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn save_ultra<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
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.
Sourcefn update_fast<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<UpdateResult, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn update_fast<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<UpdateResult, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Updates the current instance without hooks or extra safety checks.
Sourcefn update_ultra<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<UpdateResult, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn update_ultra<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<UpdateResult, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Updates the current instance using the ultra-fast path (no hooks/extra checks).
Sourcefn delete_fast<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn delete_fast<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Deletes the current instance without hooks or extra safety checks.
Sourcefn delete_ultra<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn delete_ultra<'a, E>(
&'a mut self,
executor: E,
) -> impl Future<Output = Result<(), Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
Deletes the current instance using the ultra-fast path (no hooks/extra checks).
Sourcefn sensitive_fields() -> &'static [&'static str]
fn sensitive_fields() -> &'static [&'static str]
Returns a list of fields that are considered sensitive and should be redacted in logs.
Sourcefn 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 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).
Sourcefn raw_sql<'q>(
sql: &'q str,
) -> QueryAs<'q, DB, Self, <DB as Database>::Arguments<'q>>
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.
Sourcefn raw_sql_fast<'q>(
sql: &'q str,
) -> QueryAs<'q, DB, FastRow<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>>
Use raw SQL with fast positional mapping (select columns in model field order).
Sourcefn eager_load<'a>(
_models: &mut [Self],
_relation: &str,
_executor: Executor<'a, DB>,
) -> impl Future<Output = Result<(), Error>> + Send
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.
Sourcefn find<'a, E>(executor: E) -> QueryBuilder<'a, Self, DB>where
E: IntoExecutor<'a, DB = DB>,
fn find<'a, E>(executor: E) -> QueryBuilder<'a, Self, DB>where
E: IntoExecutor<'a, DB = DB>,
Creates a new QueryBuilder for this model.
Sourcefn find_in_pool(pool: &Pool<DB>) -> QueryBuilder<'_, Self, DB>
fn find_in_pool(pool: &Pool<DB>) -> QueryBuilder<'_, Self, DB>
Creates a new QueryBuilder using a connection pool.
Sourcefn find_in_tx(conn: &mut DB::Connection) -> QueryBuilder<'_, Self, DB>
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.