pub trait Model:
Sized
+ Send
+ 'static {
type Fields;
type PrimaryKey: PrimaryKey;
const APP_NAME: &'static str;
const TABLE_NAME: Identifier;
const PRIMARY_KEY_NAME: Identifier;
const COLUMNS: &'static [Column];
// Required methods
fn from_db(db_row: Row) -> Result<Self>;
fn update_from_db(&mut self, db_row: Row, columns: &[usize]) -> Result<()>;
fn primary_key(&self) -> &Self::PrimaryKey;
fn set_primary_key(&mut self, primary_key: Self::PrimaryKey);
fn get_values(&self, columns: &[usize]) -> Vec<&dyn ToDbFieldValue>;
fn get_by_primary_key<'life0, 'async_trait, DB>(
db: &'life0 DB,
pk: Self::PrimaryKey,
) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>
where DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn objects() -> Query<Self> { ... }
fn save<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn insert<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn update<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
}db only.Expand description
A model trait for database models.
This trait is used to define a model that can be stored in a database. It is used to define the structure of the model, the table name, and the columns.
§Deriving
This trait can, and should be derived using the model attribute macro.
This macro generates the implementation of the trait for the type, including
the implementation of the Fields helper struct.
use cot::db::model;
#[model]
struct MyModel {
#[model(primary_key)]
id: i32,
name: String,
}Required Associated Constants§
Sourceconst TABLE_NAME: Identifier
const TABLE_NAME: Identifier
The name of the table in the database.
Sourceconst PRIMARY_KEY_NAME: Identifier
const PRIMARY_KEY_NAME: Identifier
The name of the primary key column in the database.
Required Associated Types§
Sourcetype Fields
type Fields
A helper structure for the fields of the model.
This structure should a constant FieldRef instance
for each field in the model. Note that the names of the fields
should be written in UPPER_SNAKE_CASE, just like other constants in
Rust.
Sourcetype PrimaryKey: PrimaryKey
type PrimaryKey: PrimaryKey
The primary key type of the model.
Required Methods§
Sourcefn from_db(db_row: Row) -> Result<Self>
fn from_db(db_row: Row) -> Result<Self>
Creates a model instance from a database row.
§Errors
This method can return an error if the data in the row is not compatible with the model.
Sourcefn primary_key(&self) -> &Self::PrimaryKey
fn primary_key(&self) -> &Self::PrimaryKey
Returns the primary key of the model.
Sourcefn set_primary_key(&mut self, primary_key: Self::PrimaryKey)
fn set_primary_key(&mut self, primary_key: Self::PrimaryKey)
Used by the ORM to set the primary key of the model after it has been saved to the database.
Sourcefn get_values(&self, columns: &[usize]) -> Vec<&dyn ToDbFieldValue>
fn get_values(&self, columns: &[usize]) -> Vec<&dyn ToDbFieldValue>
Gets the values of the model for the given columns.
Sourcefn get_by_primary_key<'life0, 'async_trait, DB>(
db: &'life0 DB,
pk: Self::PrimaryKey,
) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
fn get_by_primary_key<'life0, 'async_trait, DB>(
db: &'life0 DB,
pk: Self::PrimaryKey,
) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
Queries the database for a model instance with the given primary key.
§Errors
This method can return an error if the model instance could not be found in the database, or there was a problem with the database connection.
Provided Methods§
Sourcefn save<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn save<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Inserts the model instance to the database, or updates an instance with the same primary key if it already exists.
To force insert or force update, use the Self::insert or
Self::update methods instead.
§Errors
This method can return an error if the model instance could not be inserted into the database, for instance because the migrations haven’t been applied, or there was a problem with the database connection.
Sourcefn insert<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn insert<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Insert the model instance to the database.
§Errors
This method can return an error if the model instance could not be inserted into the database, for instance because the migrations haven’t been applied, or there was a problem with the database connection.
Sourcefn update<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn update<'life0, 'life1, 'async_trait, DB>(
&'life0 mut self,
db: &'life1 DB,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
DB: 'async_trait + DatabaseBackend,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Update the model instance in the database.
§Errors
This method can return an error if the model instance could not be inserted into the database, for instance because the migrations haven’t been applied, or there was a problem with the database connection.
This method can return an error if the model with the given primary key could not be found in the database.
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.