pub trait ModelKey:
Sealed
+ ToDbValue
+ FromDb
+ Clone
+ Eq
+ Ord
+ Hash
+ Debug
+ Display
+ FromStr
+ Default
+ Send
+ Sync
+ Unpin
+ 'static {
const AUTO_INCREMENT: bool = false;
// Required methods
fn is_unsaved(&self) -> bool;
fn generate() -> Option<Self>;
}Expand description
A type a model’s id can have: i64 (the database numbers the rows),
Ulid, uuid::Uuid (renox’s uuid feature) or String.
#[derive(Model)] takes the key type from the id field:
use renox::db::Ulid;
#[derive(Model, serde::Serialize, Default)]
#[model(table = "invoices")]
struct Invoice {
id: Ulid, // made on insert; TEXT PRIMARY KEY in the migration
total: i64,
}
let invoice = Invoice::find_or_404(&db, id).await?;A key’s empty value (0, the nil UUID, an empty string) means “not
saved yet”: save inserts such a model, making the key (a new ULID or
UUID v7) unless the database does (i64). A String key is yours to
set, so save a new row with Model::insert or
Model::create.
The trait is sealed: other types can’t be keys.
Provided Associated Constants§
Sourceconst AUTO_INCREMENT: bool = false
const AUTO_INCREMENT: bool = false
The database makes the key on insert (RETURNING id).
Required Methods§
Sourcefn is_unsaved(&self) -> bool
fn is_unsaved(&self) -> bool
Whether this is the key of a model that isn’t saved yet.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".