Skip to main content

PartialModelTrait

Trait PartialModelTrait 

Source
pub trait PartialModelTrait: FromQueryResult {
    // Required method
    fn select_cols_nested<S: QuerySelect>(
        select: S,
        prefix: Option<&str>,
        alias: Option<&'static str>,
    ) -> S;

    // Provided method
    fn select_cols<S: QuerySelect>(select: S) -> S { ... }
}
Expand description

A partial projection of a Model — a struct that holds only some of the entity’s columns, used to avoid overfetching in SELECT queries.

Derive it on a custom struct with #[derive(DerivePartialModel)] and #[sea_orm(entity = "...")], then call Select::into_partial_model to restrict the query to just those columns. Nested partial models can be composed with #[sea_orm(nested)] fields.

Required Methods§

Source

fn select_cols_nested<S: QuerySelect>( select: S, prefix: Option<&str>, alias: Option<&'static str>, ) -> S

Used when nesting these structs into each other.

Example impl

fn select_cols_nested<S: QuerySelect>(mut select: S, prefix: Option<&str>) -> S {
    if let Some(prefix) = prefix {
        for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
            let alias = format!("{prefix}{}", col.as_str());
            select = select.column_as(col, alias);
        }
    } else {
        for col in <<T::Entity as EntityTrait>::Column as Iterable>::iter() {
            select = select.column(col);
        }
    }
    select
}

Provided Methods§

Source

fn select_cols<S: QuerySelect>(select: S) -> S

Add the partial model’s columns to a QuerySelect’s projection.

No need to implement this method; implement select_cols_nested instead.

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<T: PartialModelTrait> PartialModelTrait for Option<T>

Source§

fn select_cols_nested<S: QuerySelect>( select: S, prefix: Option<&str>, alias: Option<&'static str>, ) -> S

Implementors§