Skip to main content

ModelKey

Trait ModelKey 

Source
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§

Source

const AUTO_INCREMENT: bool = false

The database makes the key on insert (RETURNING id).

Required Methods§

Source

fn is_unsaved(&self) -> bool

Whether this is the key of a model that isn’t saved yet.

Source

fn generate() -> Option<Self>

A new key for an insert, or None when the database makes it (auto-increment) or the app must set it (String).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ModelKey for String

Source§

impl ModelKey for Uuid

Available on crate feature uuid only.
Source§

fn generate() -> Option<Self>

Version 7: time-ordered, so new rows land at the end of the index and cursor pages follow creation order.

Source§

fn is_unsaved(&self) -> bool

Source§

impl ModelKey for i64

Implementors§