pub trait Model<DB>:
Sized
+ Send
+ Sync
+ Unpin
+ for<'r> FromRow<'r, <DB as Database>::Row>where
DB: Database + SqlDialect,{
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 as Database>::Row) -> Result<Self, Error>
where usize: ColumnIndex<<DB as Database>::Row>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
Self: for<'r> FromRow<'r, <DB as Database>::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 as Database>::Row>,
&'c str: for<'c> ColumnIndex<<DB as Database>::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>,
<DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>,
&'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
<DB as Database>::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 E: for<'e> IntoExecutor<'e, DB = DB> + for<'e> 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>,
<DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>,
&'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
<DB as Database>::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 as Database>::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 relation_names() -> &'static [&'static str]
fn relation_names() -> &'static [&'static str]
Returns the relation names available for eager loading.
Sourcefn default_includes() -> &'static [&'static str]
fn default_includes() -> &'static [&'static str]
Returns relations that should be eager-loaded by default.
Sourcefn from_row_fast(row: &<DB as Database>::Row) -> Result<Self, Error>
fn from_row_fast(row: &<DB as Database>::Row) -> Result<Self, Error>
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>>where
Self: Sized,
usize: ColumnIndex<<DB as Database>::Row>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
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 as Database>::Row>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
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 all<'a, E>(
executor: E,
) -> impl Future<Output = Result<Vec<Self>, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
<DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>,
&'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
<DB as Database>::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 all<'a, E>(
executor: E,
) -> impl Future<Output = Result<Vec<Self>, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
<DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>,
&'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
<DB as Database>::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.
Sourcefn find_one<'a, E>(
executor: E,
id: i32,
) -> impl Future<Output = Result<Option<Self>, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
fn find_one<'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 (alias for [find_by_id]).
Sourcefn create<E>(
executor: E,
instance: Self,
) -> impl Future<Output = Result<Self, Error>> + Sendwhere
E: for<'e> IntoExecutor<'e, DB = DB> + for<'e> Send,
fn create<E>(
executor: E,
instance: Self,
) -> impl Future<Output = Result<Self, Error>> + Sendwhere
E: for<'e> IntoExecutor<'e, DB = DB> + for<'e> Send,
Creates a new record and returns the saved instance.
Sourcefn update_by_id<'a, E>(
executor: E,
id: i32,
json_patch: Value,
) -> impl Future<Output = Result<u64, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
<DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>,
&'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
<DB as Database>::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 update_by_id<'a, E>(
executor: E,
id: i32,
json_patch: Value,
) -> impl Future<Output = Result<u64, Error>> + Sendwhere
E: IntoExecutor<'a, DB = DB>,
<DB as Database>::Arguments<'q>: for<'q> IntoArguments<'q, DB>,
&'c mut <DB as Database>::Connection: for<'c> Executor<'c, Database = DB>,
&'c str: for<'c> ColumnIndex<<DB as Database>::Row>,
<DB as Database>::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.
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 as Database>::Connection,
) -> QueryBuilder<'_, Self, DB>
fn find_in_tx( conn: &mut <DB as Database>::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".