pub struct Relation<O, R> { /* private fields */ }model-mysql or model-postgres or model-sqlite only.Expand description
How a model O reaches related rows of R: for
QueryBuilder::with, and to load the rows
of one owner (load) or of many
(load_for). Usually returned by an associated
function of the owner, so call sites name it:
impl User {
pub fn posts() -> Relation<User, Post> { Relation::has_many("user_id") }
}
let posts: Vec<Post> = User::posts().load(&pool, &user).await?;Implementations§
Source§impl<O, R> Relation<O, R>
impl<O, R> Relation<O, R>
Sourcepub fn has_many(foreign_key: &'static str) -> Relation<O, R>
pub fn has_many(foreign_key: &'static str) -> Relation<O, R>
One owner, many R: R’s column foreign_key holds the owner’s
primary key (Relation::<User, Post>::has_many("user_id")).
Sourcepub fn has_one(foreign_key: &'static str) -> Relation<O, R>
pub fn has_one(foreign_key: &'static str) -> Relation<O, R>
As has_many, at most one R per owner (the one
with the lowest primary key when there are more).
Sourcepub fn belongs_to(foreign_key: &'static str) -> Relation<O, R>
pub fn belongs_to(foreign_key: &'static str) -> Relation<O, R>
The owner’s column foreign_key holds R’s primary key
(Relation::<Post, User>::belongs_to("user_id")); NULL means
none.
Sourcepub fn belongs_to_many(
join_table: &'static str,
owner_key: &'static str,
related_key: &'static str,
) -> Relation<O, R>
pub fn belongs_to_many( join_table: &'static str, owner_key: &'static str, related_key: &'static str, ) -> Relation<O, R>
Many-to-many through join_table, whose column owner_key holds
the owner’s primary key and related_key R’s
(Relation::<Post, Tag>::belongs_to_many("post_tags", "post_id", "tag_id")). BelongsToMany adds and
removes the links.
Sourcepub fn kind(&self) -> RelationKind
pub fn kind(&self) -> RelationKind
What the relation is.
Sourcepub fn foreign_key(&self) -> &'static str
pub fn foreign_key(&self) -> &'static str
The column holding the key that links the two: R’s for
HasMany/HasOne, O’s for BelongsTo, the join table’s owner
column for BelongsToMany.
Sourcepub fn join_table(&self) -> Option<(&'static str, &'static str)>
pub fn join_table(&self) -> Option<(&'static str, &'static str)>
BelongsToMany: the join table and its column holding R’s key.
Sourcepub async fn load(&self, pool: &DbPool, owner: &O) -> Result<Vec<R>, DbError>where
R: Clone,
pub async fn load(&self, pool: &DbPool, owner: &O) -> Result<Vec<R>, DbError>where
R: Clone,
The related rows of one owner (one query).
Sourcepub async fn load_for(
&self,
pool: &DbPool,
owners: &[O],
) -> Result<Vec<Vec<R>>, DbError>where
R: Clone,
pub async fn load_for(
&self,
pool: &DbPool,
owners: &[O],
) -> Result<Vec<Vec<R>>, DbError>where
R: Clone,
The related rows of every owner, in the owners’ order — one query
per IN_BATCH distinct keys, the related rows by primary key. A
row related to several owners (a shared author, a tag) is cloned
into each.
Trait Implementations§
Auto Trait Implementations§
impl<O, R> Freeze for Relation<O, R>
impl<O, R> RefUnwindSafe for Relation<O, R>
impl<O, R> Send for Relation<O, R>
impl<O, R> Sync for Relation<O, R>
impl<O, R> Unpin for Relation<O, R>
impl<O, R> UnsafeUnpin for Relation<O, R>
impl<O, R> UnwindSafe for Relation<O, 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.