logo
pub trait FromQueryResult: Sized {
    fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, DbErr>;

    fn from_query_result_optional(
        res: &QueryResult,
        pre: &str
    ) -> Result<Option<Self>, DbErr> { ... }
fn find_by_statement(stmt: Statement) -> SelectorRaw<SelectModel<Self>> { ... } }
Expand description

A Trait for implementing a QueryResult

Required methods

Instantiate a Model from a QueryResult

Provided methods

Transform the error from instantiating a Model from a QueryResult and converting it to an Option

use sea_orm::{query::*, FromQueryResult};

#[derive(Debug, PartialEq, FromQueryResult)]
struct SelectResult {
    name: String,
    num_of_cakes: i32,
}

let res: Vec<SelectResult> = SelectResult::find_by_statement(Statement::from_sql_and_values(
    DbBackend::Postgres,
    r#"SELECT "name", COUNT(*) AS "num_of_cakes" FROM "cake" GROUP BY("name")"#,
    vec![],
))
.all(&db)
.await?;

assert_eq!(
    res,
    vec![SelectResult {
        name: "Chocolate Forest".to_owned(),
        num_of_cakes: 2,
    },]
);

Implementors