pub struct Association<T> { /* private fields */ }Expand description
A typed handle to a model association (relation).
Association represents a link between a source model and a target model,
such as a has-many or belongs-to relation. It wraps an untyped
stmt::Association and carries a type T
that encodes the returning type — what executing the association query
produces:
Association<List<M>>— a has-many relation, returnsVec<M>.Association<M>— a has-one or belongs-to relation, returnsM.
Associations are constructed by generated code (see many,
many_via_one, and one).
They implement IntoStatement so they can be passed directly to
Db::exec.
Implementations§
Source§impl<T> Association<T>
impl<T> Association<T>
Sourcepub fn untyped(&self) -> &Association
pub fn untyped(&self) -> &Association
Borrow the underlying untyped association.
Source§impl<M> Association<List<M>>where
M: Model,
impl<M> Association<List<M>>where
M: Model,
Sourcepub fn many<T>(
source: Query<List<T>>,
path: Path<T, List<M>>,
) -> Association<List<M>>where
T: Model,
pub fn many<T>(
source: Query<List<T>>,
path: Path<T, List<M>>,
) -> Association<List<M>>where
T: Model,
Create a has-many association from source following path.
§Panics
Panics if the root of path does not match the model id of T.
§Examples
use toasty::stmt::{Association, List, Query};
use toasty::schema::Model;
let source = Query::<List<User>>::all().filter(User::fields().id().eq(1));
let path = User::path_field::<List<Todo>>(2);
let _assoc = Association::many(source, path);Sourcepub fn many_via_one<T>(
source: Query<List<T>>,
path: Path<T, M>,
) -> Association<List<M>>where
T: Model,
pub fn many_via_one<T>(
source: Query<List<T>>,
path: Path<T, M>,
) -> Association<List<M>>where
T: Model,
Create a has-many association through a singular (has-one / belongs-to) path. Because the source is a query that may match multiple rows, the result is still a list.
§Panics
Panics if the root of path does not match the model id of T.
§Examples
use toasty::stmt::{Association, List, Query};
use toasty::schema::Model;
let source = Query::<List<Todo>>::all();
let path = Todo::path_field::<User>(1);
let _assoc: Association<List<User>> = Association::many_via_one(source, path);Sourcepub fn insert(self, expr: impl IntoExpr<M>) -> Statement<()>
pub fn insert(self, expr: impl IntoExpr<M>) -> Statement<()>
Insert an associated record into this has-many relation.
Converts the association into an update statement that adds expr to
the relation’s field on the source model.
§Examples
use toasty::stmt::{Association, Expr, List, Query};
use toasty::schema::Model;
let source = Query::<List<User>>::all().filter(User::fields().id().eq(1));
let path = User::path_field::<List<Todo>>(2);
let assoc = Association::many(source, path);
let todo_expr = Expr::<Todo>::from_untyped(
toasty_core::stmt::Value::from(42_i64),
);
let _stmt = assoc.insert(todo_expr);Sourcepub fn remove(self, expr: impl IntoExpr<M>) -> Statement<()>
pub fn remove(self, expr: impl IntoExpr<M>) -> Statement<()>
Remove an associated record from this has-many relation.
Converts the association into an update statement that removes expr
from the relation’s field on the source model.
§Examples
use toasty::stmt::{Association, Expr, List, Query};
use toasty::schema::Model;
let source = Query::<List<User>>::all().filter(User::fields().id().eq(1));
let path = User::path_field::<List<Todo>>(2);
let assoc = Association::many(source, path);
// Remove a todo by its expression
let todo_expr = Expr::<Todo>::from_untyped(
toasty_core::stmt::Value::from(42_i64),
);
let _stmt = assoc.remove(todo_expr);Source§impl<M> Association<M>where
M: Model,
impl<M> Association<M>where
M: Model,
Sourcepub fn one<T>(source: Query<List<T>>, path: Path<T, M>) -> Association<M>where
T: Model,
pub fn one<T>(source: Query<List<T>>, path: Path<T, M>) -> Association<M>where
T: Model,
Create a has-one or belongs-to association from source following
path.
§Panics
Panics if the root of path does not match the model id of T.
§Examples
use toasty::stmt::{Association, List, Query};
use toasty::schema::Model;
let source = Query::<List<Todo>>::all().filter(Todo::fields().id().eq(1));
let path = Todo::path_field::<User>(1);
let _assoc = Association::one(source, path);Trait Implementations§
Source§impl<M> Debug for Association<M>
impl<M> Debug for Association<M>
Source§impl<M> IntoScope<M> for Association<List<M>>where
M: Model,
impl<M> IntoScope<M> for Association<List<M>>where
M: Model,
Source§fn into_scope(self) -> Statement<List<M>>
fn into_scope(self) -> Statement<List<M>>
self into a Statement<List<M>> for use as an insert scope.Source§impl<M> IntoScope<M> for Association<M>where
M: Model,
impl<M> IntoScope<M> for Association<M>where
M: Model,
Source§fn into_scope(self) -> Statement<List<M>>
fn into_scope(self) -> Statement<List<M>>
self into a Statement<List<M>> for use as an insert scope.