Skip to main content

PivotRelation

Struct PivotRelation 

Source
pub struct PivotRelation<P: EntityTrait, R: EntityTrait, K> { /* private fields */ }
Expand description

A many-to-many relation from owners to R through the pivot entity P.

Use it as BelongsToMany or MorphToMany, declared once as a constant:

use sea_orm_relations_kit::{Morph, MorphToMany};

pub const TAGS: MorphToMany<taggables::Entity, tags::Entity> = MorphToMany::new(
    Morph::new(taggables::Column::TaggableType, taggables::Column::TaggableId),
    taggables::Column::TagId,
)
.ordered_by(taggables::Column::Position);

Writes run several statements (read the pivot, delete, insert); run them in a transaction when they must be atomic. Pivot rows are written in bulk, so the pivot entity’s own hooks do not run.

Implementations§

Source§

impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Plain>

Source

pub const fn new(owner_column: P::Column, related_column: P::Column) -> Self

owner_column points at the owner, related_column at R.

Source§

impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Morphed>

Source

pub const fn new(morph: Morph<P>, related_column: P::Column) -> Self

morph holds the owner’s type and key, related_column points at R.

Source§

impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Morphed>
where P::Model: Sync, P::ActiveModel: Send, R::Model: Sync,

Source

pub fn owners_of<O: Morphable>( &self, related: &R::Model, ) -> Result<Select<O>, DbErr>

The owners of type O attached to related: the posts of a tag.

§Errors

When related has no key, or it does not fit the pivot column.

Source

pub async fn load_owners_of<O, Db>( &self, db: &Db, related: &[R::Model], ) -> Result<Vec<Vec<O::Model>>, DbErr>
where O: Morphable, Db: ConnectionTrait,

The owners of type O of many related rows, in one query each for the pivot and the owners.

§Errors

On a database error, or when a key does not fit its column.

Source

pub async fn count_owners_of<O, Db>( &self, db: &Db, related: &[R::Model], ) -> Result<Vec<u64>, DbErr>
where O: Morphable, Db: ConnectionTrait,

How many owners of type O each related row has.

§Errors

On a database error, or when a key does not fit its column.

Source

pub fn related_of<O: Morphable>(&self, owners: Select<O>) -> Condition

For queries on R: rows attached to at least one owner owners matches.

Source§

impl<P: EntityTrait, R: EntityTrait, K> PivotRelation<P, R, K>

Source

pub const fn ordered_by(self, column: P::Column) -> Self

Keeps the attached rows in order by column, an integer column of the pivot: new rows go last, sync writes the order it is given.

Source

pub const fn owner_column(&self) -> P::Column

The pivot’s column pointing at the owner.

Source

pub const fn related_column(&self) -> P::Column

The pivot’s column pointing at R.

Source

pub const fn order_column(&self) -> Option<P::Column>

The pivot’s order column, if any.

Source§

impl<P: EntityTrait, R: EntityTrait, K> PivotRelation<P, R, K>
where P::Model: Sync, P::ActiveModel: Send, R::Model: Sync,

Source

pub fn pivot_of<O>(&self, owner: &O) -> Result<Condition, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>>,

For queries on P: the pivot rows of owner.

§Errors

When owner has no key, or it does not fit the pivot column.

Source

pub fn find_for<O>(&self, owner: &O) -> Result<Select<R>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>>,

The related rows of owner, in pivot order, ready to filter and run.

§Errors

When owner has no key, or it does not fit the pivot column.

Source

pub async fn keys_for<O, Db>( &self, db: &Db, owner: &O, ) -> Result<Vec<Key>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

The keys of owner’s related rows, in pivot order.

§Errors

On a database error, or when owner’s key does not fit the pivot column.

Source

pub async fn pivots_for<O, Db>( &self, db: &Db, owner: &O, ) -> Result<Vec<P::Model>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

The pivot rows of owner, in pivot order, to read extra pivot columns.

§Errors

On a database error, or when owner’s key does not fit the pivot column.

Source

pub async fn load_for<O, Db>( &self, db: &Db, owners: &[O], ) -> Result<Vec<Vec<R::Model>>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

The related rows of many owners, two queries in all, each list in pivot order.

§Errors

On a database error, or when an owner’s key does not fit the pivot column.

Source

pub async fn load_for_with<O, Db>( &self, db: &Db, owners: &[O], related: Select<R>, ) -> Result<Vec<Vec<R::Model>>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

load_for keeping only the related rows related matches.

§Errors

On a database error, or when an owner’s key does not fit the pivot column.

Source

pub async fn count_for<O, Db>( &self, db: &Db, owners: &[O], ) -> Result<Vec<u64>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

How many related rows each owner has, in one GROUP BY query; 0 for none.

§Errors

On a database error, or when an owner’s key does not fit the pivot column.

Source

pub async fn attach<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Vec<Key>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

Attaches keys to owner, skipping those already attached; new rows go last. Returns the keys that were attached, so calling it twice is safe.

§Errors

On a database error, or when a key does not fit its column.

Source

pub async fn attach_with<O, Db, F>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, fill: F, ) -> Result<Vec<Key>, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait, F: Fn(&mut P::ActiveModel) -> Result<(), DbErr> + Sync,

attach, filling extra pivot columns of each new row.

§Errors

On a database error, when a key does not fit its column, or when fill fails.

Source

pub async fn detach<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<u64, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

Detaches keys from owner; returns how many rows went.

§Errors

On a database error, or when a key does not fit its column.

Source

pub async fn detach_all<O, Db>(&self, db: &Db, owner: &O) -> Result<u64, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

Detaches everything from owner, for its delete hook.

§Errors

On a database error, or when owner’s key does not fit the pivot column.

Source

pub async fn sync<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Changes, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

Makes keys, in this order, exactly what owner has attached.

§Errors

On a database error, or when a key does not fit its column.

Source

pub async fn sync_with<O, Db, F>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, fill: F, ) -> Result<Changes, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait, F: Fn(&mut P::ActiveModel) -> Result<(), DbErr> + Sync,

sync, filling extra pivot columns of the newly attached rows.

§Errors

On a database error, when a key does not fit its column, or when fill fails.

Source

pub async fn sync_without_detaching<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Changes, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

Attaches the keys not yet attached and keeps the rest, in their order.

§Errors

On a database error, or when a key does not fit its column.

Source

pub async fn toggle<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Changes, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait,

Detaches the keys that are attached and attaches the rest.

§Errors

On a database error, or when a key does not fit its column.

Source

pub async fn update_pivot<O, Db, F>( &self, db: &Db, owner: &O, key: impl Into<Key> + Send, change: F, ) -> Result<u64, DbErr>
where O: ModelTrait<Entity: PivotOwner<K>> + Sync, Db: ConnectionTrait, F: FnOnce(UpdateMany<P>) -> UpdateMany<P> + Send,

Updates the pivot row linking owner and key; returns how many rows changed:

use sea_orm::{sea_query::Expr, DatabaseConnection, DbErr};
use sea_orm_relations_kit::BelongsToMany;

const ROLES: BelongsToMany<role_user::Entity, roles::Entity> =
    BelongsToMany::new(role_user::Column::UserId, role_user::Column::RoleId);

async fn make_lead(db: &DatabaseConnection, user: &users::Model, role: &roles::Model) -> Result<(), DbErr> {
    ROLES
        .update_pivot(db, user, role.id, |update| {
            update.col_expr(role_user::Column::Note, Expr::value("lead"))
        })
        .await?;
    Ok(())
}
§Errors

On a database error, or when a key does not fit its column.

Source

pub fn owners_with<O: PivotOwner<K>>(&self, related: Select<R>) -> Condition

For queries on O: owners attached to at least one row related matches; pass R::find() for owners with anything attached.

Source

pub fn owners_without<O: PivotOwner<K>>(&self, related: Select<R>) -> Condition

For queries on O: owners attached to no row related matches.

Source

pub fn owners_with_all<O: PivotOwner<K>>( &self, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Condition, DbErr>

For queries on O: owners attached to every one of keys.

§Errors

When a key does not fit the pivot column.

Source

pub async fn delete_orphans<O, Db>(&self, db: &Db) -> Result<u64, DbErr>
where O: PivotOwner<K>, Db: ConnectionTrait,

Deletes pivot rows of owners of type O that no longer exist.

§Errors

On a database error.

Trait Implementations§

Source§

impl<P: Clone + EntityTrait, R: Clone + EntityTrait, K: Clone> Clone for PivotRelation<P, R, K>
where P::Column: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Copy + EntityTrait, R: Copy + EntityTrait, K: Copy> Copy for PivotRelation<P, R, K>
where P::Column: Copy,

Source§

impl<P: Debug + EntityTrait, R: Debug + EntityTrait, K: Debug> Debug for PivotRelation<P, R, K>
where P::Column: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<P, R, K> Forget for PivotRelation<P, R, K>

§

impl<P, R, K> Freeze for PivotRelation<P, R, K>

§

impl<P, R, K> Move for PivotRelation<P, R, K>

§

impl<P, R, K> RefUnwindSafe for PivotRelation<P, R, K>

§

impl<P, R, K> Send for PivotRelation<P, R, K>
where <P as EntityTrait>::Column: Send,

§

impl<P, R, K> Sync for PivotRelation<P, R, K>
where <P as EntityTrait>::Column: Sync,

§

impl<P, R, K> Unpin for PivotRelation<P, R, K>
where <P as EntityTrait>::Column: Unpin,

§

impl<P, R, K> UnsafeUnpin for PivotRelation<P, R, K>

§

impl<P, R, K> UnwindSafe for PivotRelation<P, R, K>
where <P as EntityTrait>::Column: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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