Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync
    + Unpin
    + 'static
where for<'r> Self: 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: &PgPool, id: Self::PrimaryKey, ) -> BoxFuture<'_, Result<Option<Self>, Error>> { ... } fn all(pool: &PgPool) -> BoxFuture<'_, Result<Vec<Self>, Error>> { ... } }

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: &PgPool, id: Self::PrimaryKey, ) -> BoxFuture<'_, Result<Option<Self>, Error>>

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: &PgPool) -> BoxFuture<'_, Result<Vec<Self>, Error>>

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§