pub struct BelongsToMany<R>where
R: Model,{ /* private fields */ }model-mysql or model-postgres or model-sqlite only.Expand description
Many-to-many through a join table (18.30.0): the rows of R one owner
is linked to, and the links themselves — attach,
detach, sync.
The join table holds the owner’s key in owner_key and R’s in
related_key; give it a primary key or unique index over both (the
methods never insert a link twice, but two processes attaching at once
could). Other columns need defaults: the methods insert the two keys
only. Relation::belongs_to_many
loads the same links for a whole result set.
impl Post {
pub fn tags(&self) -> BelongsToMany<Tag> {
BelongsToMany::new(Value::Int(self.id), "post_tags", "post_id", "tag_id")
}
}
post.tags().attach(&pool, [1, 2]).await?;
post.tags().sync(&pool, [1, 3]).await?; // detaches 2, attaches 3
let tags: Vec<Tag> = post.tags().load(&pool).await?;Implementations§
Source§impl<R> BelongsToMany<R>where
R: Model,
impl<R> BelongsToMany<R>where
R: Model,
Sourcepub fn new(
owner_pk: Value,
join_table: &'static str,
owner_key: &'static str,
related_key: &'static str,
) -> BelongsToMany<R>
pub fn new( owner_pk: Value, join_table: &'static str, owner_key: &'static str, related_key: &'static str, ) -> BelongsToMany<R>
The links of the owner with key owner_pk in join_table.
Sourcepub async fn load(&self, pool: &DbPool) -> Result<Vec<R>, DbError>
pub async fn load(&self, pool: &DbPool) -> Result<Vec<R>, DbError>
The linked rows of R (soft-deleted ones left out), by primary key.
Sourcepub async fn ids(&self, pool: &DbPool) -> Result<Vec<Value>, DbError>
pub async fn ids(&self, pool: &DbPool) -> Result<Vec<Value>, DbError>
The related keys linked to the owner, as the join table holds them.
Sourcepub async fn attach<I>(&self, pool: &DbPool, ids: I) -> Result<u64, DbError>
pub async fn attach<I>(&self, pool: &DbPool, ids: I) -> Result<u64, DbError>
Link the owner to each of ids it isn’t linked to yet, in one
transaction; answers how many links were added.
Sourcepub async fn detach<I>(&self, pool: &DbPool, ids: I) -> Result<u64, DbError>
pub async fn detach<I>(&self, pool: &DbPool, ids: I) -> Result<u64, DbError>
Unlink the owner from each of ids; answers how many links were
removed.
Sourcepub async fn detach_all(&self, pool: &DbPool) -> Result<u64, DbError>
pub async fn detach_all(&self, pool: &DbPool) -> Result<u64, DbError>
Unlink the owner from every row; answers how many links were removed.
Trait Implementations§
Source§impl<R> Clone for BelongsToMany<R>where
R: Model,
impl<R> Clone for BelongsToMany<R>where
R: Model,
Source§fn clone(&self) -> BelongsToMany<R>
fn clone(&self) -> BelongsToMany<R>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<R> Freeze for BelongsToMany<R>
impl<R> RefUnwindSafe for BelongsToMany<R>
impl<R> Send for BelongsToMany<R>
impl<R> Sync for BelongsToMany<R>
impl<R> Unpin for BelongsToMany<R>
impl<R> UnsafeUnpin for BelongsToMany<R>
impl<R> UnwindSafe for BelongsToMany<R>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
map-response-body only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.