pub trait FromQueryResult: Sized {
// Required method
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, DbErr>;
// Provided methods
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§
Sourcefn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, DbErr>
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, DbErr>
Instantiate a Model from a QueryResult
Provided Methods§
Sourcefn from_query_result_optional(
res: &QueryResult,
pre: &str,
) -> Result<Option<Self>, DbErr>
fn from_query_result_optional( res: &QueryResult, pre: &str, ) -> Result<Option<Self>, DbErr>
Transform the error from instantiating a Model from a QueryResult and converting it to an Option
Sourcefn find_by_statement(stmt: Statement) -> SelectorRaw<SelectModel<Self>>
fn find_by_statement(stmt: Statement) -> SelectorRaw<SelectModel<Self>>
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")"#,
[],
))
.all(&db)
.await?;
assert_eq!(
res,
[SelectResult {
name: "Chocolate Forest".to_owned(),
num_of_cakes: 2,
},]
);
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.