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>
impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Plain>
Source§impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Morphed>
impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Morphed>
Source§impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Morphed>
impl<P: EntityTrait, R: EntityTrait> PivotRelation<P, R, Morphed>
Sourcepub fn owners_of<O: Morphable>(
&self,
related: &R::Model,
) -> Result<Select<O>, DbErr>
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.
Sourcepub async fn load_owners_of<O, Db>(
&self,
db: &Db,
related: &[R::Model],
) -> Result<Vec<Vec<O::Model>>, DbErr>where
O: Morphable,
Db: ConnectionTrait,
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.
Sourcepub async fn count_owners_of<O, Db>(
&self,
db: &Db,
related: &[R::Model],
) -> Result<Vec<u64>, DbErr>where
O: Morphable,
Db: ConnectionTrait,
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.
For queries on R: rows attached to at least one owner owners matches.
Source§impl<P: EntityTrait, R: EntityTrait, K> PivotRelation<P, R, K>
impl<P: EntityTrait, R: EntityTrait, K> PivotRelation<P, R, K>
Sourcepub const fn ordered_by(self, column: P::Column) -> Self
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.
Sourcepub const fn owner_column(&self) -> P::Column
pub const fn owner_column(&self) -> P::Column
The pivot’s column pointing at the owner.
The pivot’s column pointing at R.
Sourcepub const fn order_column(&self) -> Option<P::Column>
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>
impl<P: EntityTrait, R: EntityTrait, K> PivotRelation<P, R, K>
Sourcepub fn pivot_of<O>(&self, owner: &O) -> Result<Condition, DbErr>where
O: ModelTrait<Entity: PivotOwner<K>>,
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.
Sourcepub fn find_for<O>(&self, owner: &O) -> Result<Select<R>, DbErr>where
O: ModelTrait<Entity: PivotOwner<K>>,
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.
Sourcepub async fn keys_for<O, Db>(
&self,
db: &Db,
owner: &O,
) -> Result<Vec<Key>, DbErr>
pub async fn keys_for<O, Db>( &self, db: &Db, owner: &O, ) -> Result<Vec<Key>, DbErr>
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.
Sourcepub async fn pivots_for<O, Db>(
&self,
db: &Db,
owner: &O,
) -> Result<Vec<P::Model>, DbErr>
pub async fn pivots_for<O, Db>( &self, db: &Db, owner: &O, ) -> Result<Vec<P::Model>, DbErr>
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.
Sourcepub async fn load_for<O, Db>(
&self,
db: &Db,
owners: &[O],
) -> Result<Vec<Vec<R::Model>>, DbErr>
pub async fn load_for<O, Db>( &self, db: &Db, owners: &[O], ) -> Result<Vec<Vec<R::Model>>, DbErr>
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.
Sourcepub async fn load_for_with<O, Db>(
&self,
db: &Db,
owners: &[O],
related: Select<R>,
) -> Result<Vec<Vec<R::Model>>, DbErr>
pub async fn load_for_with<O, Db>( &self, db: &Db, owners: &[O], related: Select<R>, ) -> Result<Vec<Vec<R::Model>>, DbErr>
Sourcepub async fn count_for<O, Db>(
&self,
db: &Db,
owners: &[O],
) -> Result<Vec<u64>, DbErr>
pub async fn count_for<O, Db>( &self, db: &Db, owners: &[O], ) -> Result<Vec<u64>, DbErr>
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.
Sourcepub async fn attach<O, Db>(
&self,
db: &Db,
owner: &O,
keys: impl IntoIterator<Item = impl Into<Key>> + Send,
) -> Result<Vec<Key>, DbErr>
pub async fn attach<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Vec<Key>, DbErr>
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.
Sourcepub 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,
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,
Sourcepub async fn detach<O, Db>(
&self,
db: &Db,
owner: &O,
keys: impl IntoIterator<Item = impl Into<Key>> + Send,
) -> Result<u64, DbErr>
pub async fn detach<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<u64, DbErr>
Detaches keys from owner; returns how many rows went.
§Errors
On a database error, or when a key does not fit its column.
Sourcepub async fn detach_all<O, Db>(&self, db: &Db, owner: &O) -> Result<u64, DbErr>
pub async fn detach_all<O, Db>(&self, db: &Db, owner: &O) -> Result<u64, DbErr>
Detaches everything from owner, for its delete hook.
§Errors
On a database error, or when owner’s key does not fit the pivot column.
Sourcepub async fn sync<O, Db>(
&self,
db: &Db,
owner: &O,
keys: impl IntoIterator<Item = impl Into<Key>> + Send,
) -> Result<Changes, DbErr>
pub async fn sync<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Changes, DbErr>
Makes keys, in this order, exactly what owner has attached.
§Errors
On a database error, or when a key does not fit its column.
Sourcepub 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,
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,
Sourcepub async fn sync_without_detaching<O, Db>(
&self,
db: &Db,
owner: &O,
keys: impl IntoIterator<Item = impl Into<Key>> + Send,
) -> Result<Changes, DbErr>
pub async fn sync_without_detaching<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Changes, DbErr>
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.
Sourcepub async fn toggle<O, Db>(
&self,
db: &Db,
owner: &O,
keys: impl IntoIterator<Item = impl Into<Key>> + Send,
) -> Result<Changes, DbErr>
pub async fn toggle<O, Db>( &self, db: &Db, owner: &O, keys: impl IntoIterator<Item = impl Into<Key>> + Send, ) -> Result<Changes, DbErr>
Detaches the keys that are attached and attaches the rest.
§Errors
On a database error, or when a key does not fit its column.
Sourcepub 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,
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.
Sourcepub fn owners_with<O: PivotOwner<K>>(&self, related: Select<R>) -> Condition
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.
Sourcepub fn owners_without<O: PivotOwner<K>>(&self, related: Select<R>) -> Condition
pub fn owners_without<O: PivotOwner<K>>(&self, related: Select<R>) -> Condition
For queries on O: owners attached to no row related matches.
Sourcepub fn owners_with_all<O: PivotOwner<K>>(
&self,
keys: impl IntoIterator<Item = impl Into<Key>> + Send,
) -> Result<Condition, DbErr>
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.
Sourcepub async fn delete_orphans<O, Db>(&self, db: &Db) -> Result<u64, DbErr>where
O: PivotOwner<K>,
Db: ConnectionTrait,
pub async fn delete_orphans<O, Db>(&self, db: &Db) -> Result<u64, DbErr>where
O: PivotOwner<K>,
Db: ConnectionTrait,
Trait Implementations§
Source§impl<P: Clone + EntityTrait, R: Clone + EntityTrait, K: Clone> Clone for PivotRelation<P, R, K>
impl<P: Clone + EntityTrait, R: Clone + EntityTrait, K: Clone> Clone for PivotRelation<P, R, K>
impl<P: Copy + EntityTrait, R: Copy + EntityTrait, K: Copy> Copy for PivotRelation<P, R, K>
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>
impl<P, R, K> Sync for PivotRelation<P, R, K>
impl<P, R, K> Unpin for PivotRelation<P, R, K>
impl<P, R, K> UnsafeUnpin for PivotRelation<P, R, K>
impl<P, R, K> UnwindSafe for PivotRelation<P, R, K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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