Skip to main content

Association

Struct Association 

Source
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, returns Vec<M>.
  • Association<M> — a has-one or belongs-to relation, returns M.

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>

Source

pub fn untyped(&self) -> &Association

Borrow the underlying untyped association.

Source§

impl<M> Association<List<M>>
where M: Model,

Source

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);
Source

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);
Source

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);
Source

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,

Source

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>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<M> IntoScope<M> for Association<List<M>>
where M: Model,

Source§

fn into_scope(self) -> Statement<List<M>>

Lower self into a Statement<List<M>> for use as an insert scope.
Source§

impl<M> IntoScope<M> for Association<M>
where M: Model,

Source§

fn into_scope(self) -> Statement<List<M>>

Lower self into a Statement<List<M>> for use as an insert scope.
Source§

impl<T> IntoStatement for Association<List<T>>
where T: Model,

Source§

type Returning = List<T>

The type this statement produces when executed.
Source§

fn into_statement(self) -> Statement<List<T>>

Consume self and produce the Statement.
Source§

impl<T> IntoStatement for Association<T>
where T: Model,

Source§

type Returning = List<T>

The type this statement produces when executed.
Source§

fn into_statement(self) -> Statement<List<T>>

Consume self and produce the Statement.

Auto Trait Implementations§

§

impl<T> Freeze for Association<T>
where PhantomData<T>: Freeze,

§

impl<T> RefUnwindSafe for Association<T>

§

impl<T> Send for Association<T>
where PhantomData<T>: Send,

§

impl<T> Sync for Association<T>
where PhantomData<T>: Sync,

§

impl<T> Unpin for Association<T>
where PhantomData<T>: Unpin,

§

impl<T> UnsafeUnpin for Association<T>

§

impl<T> UnwindSafe for Association<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more