pub struct Morph<C: EntityTrait> { /* private fields */ }Expand description
The *_type and *_id columns of a polymorphic child: commentable_type and
commentable_id on comments.
Declare it once per relation, as a constant next to the child entity:
use sea_orm_relations_kit::Morph;
pub const COMMENTABLE: Morph<comments::Entity> =
Morph::new(comments::Column::CommentableType, comments::Column::CommentableId);From the owner it finds the children (find_for,
load_for); from a child it finds the owner
(owner, load_owners).
Implementations§
Source§impl<C: EntityTrait> Morph<C>
impl<C: EntityTrait> Morph<C>
Source§impl<C: EntityTrait> Morph<C>
impl<C: EntityTrait> Morph<C>
Sourcepub fn associate<O>(
&self,
child: &mut C::ActiveModel,
owner: &O,
) -> Result<(), DbErr>where
O: ModelTrait<Entity: Morphable>,
pub fn associate<O>(
&self,
child: &mut C::ActiveModel,
owner: &O,
) -> Result<(), DbErr>where
O: ModelTrait<Entity: Morphable>,
Points child at owner by setting both columns.
§Errors
When owner’s key does not fit the *_id column, or owner has no key yet.
Sourcepub fn dissociate(&self, child: &mut C::ActiveModel) -> Result<(), DbErr>
pub fn dissociate(&self, child: &mut C::ActiveModel) -> Result<(), DbErr>
Sets both columns to NULL, for a relation where the owner is optional.
§Errors
When a column is not nullable in the database; the error comes on save.
Sourcepub fn owner_type(&self, child: &C::Model) -> Option<String>
pub fn owner_type(&self, child: &C::Model) -> Option<String>
The owner’s morph type stored on child, None when it is NULL.
Sourcepub fn owner_key(&self, child: &C::Model) -> Option<Key>
pub fn owner_key(&self, child: &C::Model) -> Option<Key>
The owner’s key stored on child, None when it is NULL.
Sourcepub fn is_owned_by<O>(&self, child: &C::Model, owner: &O) -> boolwhere
O: ModelTrait<Entity: Morphable>,
pub fn is_owned_by<O>(&self, child: &C::Model, owner: &O) -> boolwhere
O: ModelTrait<Entity: Morphable>,
Whether child belongs to owner.
Sourcepub async fn owner<T, Db>(
&self,
db: &Db,
child: &C::Model,
) -> Result<Option<T>, DbErr>where
T: MorphTarget,
Db: ConnectionTrait,
pub async fn owner<T, Db>(
&self,
db: &Db,
child: &C::Model,
) -> Result<Option<T>, DbErr>where
T: MorphTarget,
Db: ConnectionTrait,
The owner of child, whichever entity it is.
T is an enum of the possible owners, declared with
morph_target!. None when the columns are NULL or
the owner row is gone.
§Errors
On a database error, or when the stored type is not one of T’s.
Sourcepub async fn load_owners<T, Db>(
&self,
db: &Db,
children: &[C::Model],
) -> Result<Vec<Option<T>>, DbErr>
pub async fn load_owners<T, Db>( &self, db: &Db, children: &[C::Model], ) -> Result<Vec<Option<T>>, DbErr>
The owners of many children, one query per owner type, in the order of
children. Children sharing an owner get a clone of it.
§Errors
On a database error, or when a stored type is not one of T’s.
Sourcepub fn of<O>(&self, owner: &O) -> Result<Condition, DbErr>where
O: ModelTrait<Entity: Morphable>,
pub fn of<O>(&self, owner: &O) -> Result<Condition, DbErr>where
O: ModelTrait<Entity: Morphable>,
WHERE type = 'post' AND id = 15: the children of owner, for any query on
C.
§Errors
When owner’s key does not fit the *_id column.
Sourcepub fn of_type<O: Morphable>(&self) -> Condition
pub fn of_type<O: Morphable>(&self) -> Condition
WHERE type = 'post': children of any owner of type O.
Sourcepub fn of_types<'a>(
&self,
morph_types: impl IntoIterator<Item = &'a str>,
) -> Condition
pub fn of_types<'a>( &self, morph_types: impl IntoIterator<Item = &'a str>, ) -> Condition
WHERE type IN (...): children of owners of the given types.
Sourcepub fn find_for<O>(&self, owner: &O) -> Result<Select<C>, DbErr>where
O: ModelTrait<Entity: Morphable>,
pub fn find_for<O>(&self, owner: &O) -> Result<Select<C>, DbErr>where
O: ModelTrait<Entity: Morphable>,
The children of owner, ready to filter, order and run.
For a one-to-one relation take .one(db); for the latest child order by a
column descending first.
§Errors
When owner’s key does not fit the *_id column.
Sourcepub async fn load_for<O, Db>(
&self,
db: &Db,
owners: &[O],
) -> Result<Vec<Vec<C::Model>>, DbErr>
pub async fn load_for<O, Db>( &self, db: &Db, owners: &[O], ) -> Result<Vec<Vec<C::Model>>, DbErr>
The children of many owners in one query, in the order of owners, without a
query per owner.
§Errors
On a database error, or when an owner’s key does not fit the *_id column.
Sourcepub async fn load_for_with<O, Db>(
&self,
db: &Db,
owners: &[O],
children: Select<C>,
) -> Result<Vec<Vec<C::Model>>, DbErr>
pub async fn load_for_with<O, Db>( &self, db: &Db, owners: &[O], children: Select<C>, ) -> Result<Vec<Vec<C::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 children 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 *_id column.
Sourcepub async fn count_for_with<O, Db>(
&self,
db: &Db,
owners: &[O],
children: Select<C>,
) -> Result<Vec<u64>, DbErr>
pub async fn count_for_with<O, Db>( &self, db: &Db, owners: &[O], children: Select<C>, ) -> Result<Vec<u64>, DbErr>
Sourcepub async fn aggregate_for<O, Db>(
&self,
db: &Db,
owners: &[O],
column: C::Column,
aggregate: Aggregate,
) -> Result<Vec<Option<f64>>, DbErr>
pub async fn aggregate_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, aggregate: Aggregate, ) -> Result<Vec<Option<f64>>, DbErr>
SUM, AVG, MIN or MAX of column over each owner’s children, as f64;
None for an owner without children.
§Errors
On a database error, or when an owner’s key does not fit the *_id column.
Sourcepub async fn pick_for<O, Db>(
&self,
db: &Db,
owners: &[O],
column: C::Column,
pick: Pick,
) -> Result<Vec<Option<C::Model>>, DbErr>
pub async fn pick_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, pick: Pick, ) -> Result<Vec<Option<C::Model>>, DbErr>
Sourcepub async fn latest_for<O, Db>(
&self,
db: &Db,
owners: &[O],
column: C::Column,
) -> Result<Vec<Option<C::Model>>, DbErr>
pub async fn latest_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, ) -> Result<Vec<Option<C::Model>>, DbErr>
Sourcepub async fn oldest_for<O, Db>(
&self,
db: &Db,
owners: &[O],
column: C::Column,
) -> Result<Vec<Option<C::Model>>, DbErr>
pub async fn oldest_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, ) -> Result<Vec<Option<C::Model>>, DbErr>
Sourcepub async fn delete_for<O, Db>(&self, db: &Db, owner: &O) -> Result<u64, DbErr>
pub async fn delete_for<O, Db>(&self, db: &Db, owner: &O) -> Result<u64, DbErr>
Deletes the children of owner, for its delete hook: polymorphic columns have
no foreign key, so the database cannot cascade. Runs one DELETE, so the
children’s own hooks do not run. Returns how many rows went.
§Errors
On a database error, or when owner’s key does not fit the *_id column.
Sourcepub async fn delete_orphans<O, Db>(&self, db: &Db) -> Result<u64, DbErr>where
O: Morphable,
Db: ConnectionTrait,
pub async fn delete_orphans<O, Db>(&self, db: &Db) -> Result<u64, DbErr>where
O: Morphable,
Db: ConnectionTrait,
Deletes children of type O whose owner row no longer exists. Returns how
many rows went.
§Errors
On a database error.
Sourcepub fn owned_by<O: Morphable>(&self, owners: Select<O>) -> Condition
pub fn owned_by<O: Morphable>(&self, owners: Select<O>) -> Condition
For queries on C: children whose owner is an O that owners matches.
use sea_orm::{DbBackend, EntityTrait, QueryFilter, QueryTrait};
use sea_orm::entity::prelude::*;
let on_published_posts = comments::Entity::find().filter(
COMMENTABLE.owned_by(posts::Entity::find().filter(posts::Column::Published.eq(true))),
);
assert_eq!(
on_published_posts.build(DbBackend::Postgres).to_string(),
r#"SELECT "comments"."id", "comments"."commentable_type", "comments"."commentable_id" FROM "comments" WHERE "comments"."commentable_type" = 'post' AND "comments"."commentable_id" IN (SELECT "posts"."id" FROM "posts" WHERE "posts"."published" = TRUE)"#
);Sourcepub fn not_owned_by<O: Morphable>(&self, owners: Select<O>) -> Condition
pub fn not_owned_by<O: Morphable>(&self, owners: Select<O>) -> Condition
For queries on C: children whose owner is an O that owners does not
match, including owners that no longer exist.
Sourcepub fn owners_with<O: Morphable>(&self, children: Select<C>) -> Condition
pub fn owners_with<O: Morphable>(&self, children: Select<C>) -> Condition
For queries on O: owners with at least one child that children matches;
pass C::find() for owners with any child.
Sourcepub fn owners_without<O: Morphable>(&self, children: Select<C>) -> Condition
pub fn owners_without<O: Morphable>(&self, children: Select<C>) -> Condition
For queries on O: owners without any child that children matches.
Trait Implementations§
impl<C: Copy + EntityTrait> Copy for Morph<C>
Auto Trait Implementations§
impl<C> Forget for Morph<C>
impl<C> Freeze for Morph<C>
impl<C> Move for Morph<C>
impl<C> RefUnwindSafe for Morph<C>
impl<C> Send for Morph<C>
impl<C> Sync for Morph<C>
impl<C> Unpin for Morph<C>
impl<C> UnsafeUnpin for Morph<C>
impl<C> UnwindSafe for Morph<C>
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