Model

Trait Model 

Source
pub trait Model<DB>:
    Sized
    + Send
    + Sync
    + Unpin
where DB: SqlDialect + Database, for<'r> Self: FromRow<'r, DB::Row>,
{ // Required methods fn table_name() -> &'static str; fn create_table_sql() -> String; fn list_columns() -> Vec<String>; async fn save<'e, E>(&mut self, executor: E) -> Result<(), Error> where E: SqlxExecutor<'e, Database = DB>; async fn update( &mut self, executor: Executor<'_, DB>, ) -> Result<UpdateResult, Error>; async fn delete(&mut self, executor: Executor<'_, DB>) -> Result<(), Error>; fn has_soft_delete() -> bool; async fn find_by_id<'e, E>( executor: E, id: i32, ) -> Result<Option<Self>, Error> where E: SqlxExecutor<'e, Database = DB>; // Provided methods async fn eager_load<'e, E>( _models: &mut [Self], _relation: &str, _executor: E, ) -> Result<(), Error> where E: SqlxExecutor<'e, Database = DB> { ... } fn find(executor: Executor<'_, DB>) -> QueryBuilder<'_, Self, DB> { ... } fn find_in_pool(pool: &Pool<DB>) -> QueryBuilder<'_, Self, DB> { ... } fn find_in_tx(conn: &mut DB::Connection) -> QueryBuilder<'_, Self, DB> { ... } }

Required Methods§

Source

fn table_name() -> &'static str

Source

fn create_table_sql() -> String

Source

fn list_columns() -> Vec<String>

Source

async fn save<'e, E>(&mut self, executor: E) -> Result<(), Error>
where E: SqlxExecutor<'e, Database = DB>,

Saves the current instance to the database.

If the record is new (no primary key), it performs an INSERT.

§Example
let mut user = User { id: 1, name: "Alice".into() };
user.save(&pool).await?;
Source

async fn update( &mut self, executor: Executor<'_, DB>, ) -> Result<UpdateResult, Error>

Source

async fn delete(&mut self, executor: Executor<'_, DB>) -> Result<(), Error>

Source

fn has_soft_delete() -> bool

Source

async fn find_by_id<'e, E>(executor: E, id: i32) -> Result<Option<Self>, Error>
where E: SqlxExecutor<'e, Database = DB>,

Finds a record by its Primary Key.

§Example
let user = User::find_by_id(&pool, 1).await?;

Provided Methods§

Source

async fn eager_load<'e, E>( _models: &mut [Self], _relation: &str, _executor: E, ) -> Result<(), Error>
where E: SqlxExecutor<'e, Database = DB>,

Source

fn find(executor: Executor<'_, DB>) -> QueryBuilder<'_, Self, DB>

Source

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

Source

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

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§