Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync
    + Unpin
    + 'static
    + for<'r> FromRow<'r, PgRow> {
    type PrimaryKey: Type<Postgres> + for<'q> Encode<'q, Postgres> + Send + Sync + Clone + 'static;

    const TABLE: &'static str;
    const COLUMNS: &'static [&'static str];
    const SOFT_DELETES: bool = false;
    const PK_COLUMN: &'static str = "id";

    // Required method
    fn primary_key(&self) -> &Self::PrimaryKey;

    // Provided methods
    fn query() -> QueryBuilder<Self> { ... }
    fn find(
        pool: &Pool<Postgres>,
        id: Self::PrimaryKey,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self>, Error>> + Send + '_>> { ... }
    fn all(
        pool: &Pool<Postgres>,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, Error>> + Send + '_>> { ... }
}

Required Associated Constants§

Source

const TABLE: &'static str

Table name (e.g. "users").

Source

const COLUMNS: &'static [&'static str]

All column names in this model’s table.

Provided Associated Constants§

Source

const SOFT_DELETES: bool = false

When true, the query builder automatically applies a deleted_at IS NULL filter to Model::query(). Set via #[soft_deletes] on the struct.

Source

const PK_COLUMN: &'static str = "id"

Primary key column name (e.g. "id").

Required Associated Types§

Source

type PrimaryKey: Type<Postgres> + for<'q> Encode<'q, Postgres> + Send + Sync + Clone + 'static

Required Methods§

Source

fn primary_key(&self) -> &Self::PrimaryKey

Return this row’s primary key.

Provided Methods§

Source

fn query() -> QueryBuilder<Self>

Start a new query builder for this model.

Source

fn find( pool: &Pool<Postgres>, id: Self::PrimaryKey, ) -> Pin<Box<dyn Future<Output = Result<Option<Self>, Error>> + Send + '_>>

Fetch by primary key. Takes a Postgres pool directly — Cast Models are Postgres-only in v0.1. For multi-driver schema + raw sqlx, use the cast::Pool enum.

Source

fn all( pool: &Pool<Postgres>, ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, Error>> + Send + '_>>

Fetch all rows.

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§