Skip to main content

Morph

Struct Morph 

Source
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>

Source

pub const fn new(type_column: C::Column, id_column: C::Column) -> Self

The relation stored in type_column and id_column of C.

Source

pub const fn type_column(&self) -> C::Column

The *_type column.

Source

pub const fn id_column(&self) -> C::Column

The *_id column.

Source§

impl<C: EntityTrait> Morph<C>
where C::Model: Sync,

Source

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.

Source

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.

Source

pub fn owner_type(&self, child: &C::Model) -> Option<String>

The owner’s morph type stored on child, None when it is NULL.

Source

pub fn owner_key(&self, child: &C::Model) -> Option<Key>

The owner’s key stored on child, None when it is NULL.

Source

pub fn is_owned_by<O>(&self, child: &C::Model, owner: &O) -> bool
where O: ModelTrait<Entity: Morphable>,

Whether child belongs to owner.

Source

pub async fn owner<T, Db>( &self, db: &Db, child: &C::Model, ) -> Result<Option<T>, DbErr>

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.

Source

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.

Source

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.

Source

pub fn of_type<O: Morphable>(&self) -> Condition

WHERE type = 'post': children of any owner of type O.

Source

pub fn of_types<'a>( &self, morph_types: impl IntoIterator<Item = &'a str>, ) -> Condition

WHERE type IN (...): children of owners of the given types.

Source

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.

Source

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

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.

Source

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

load_for with your own query for the children, to filter or order them.

§Errors

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

Source

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

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.

Source

pub async fn count_for_with<O, Db>( &self, db: &Db, owners: &[O], children: Select<C>, ) -> Result<Vec<u64>, DbErr>
where O: ModelTrait<Entity: Morphable> + Sync, Db: ConnectionTrait,

count_for counting only the children children matches.

§Errors

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

Source

pub async fn aggregate_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, aggregate: Aggregate, ) -> Result<Vec<Option<f64>>, DbErr>
where O: ModelTrait<Entity: Morphable> + Sync, Db: ConnectionTrait,

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.

Source

pub async fn pick_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, pick: Pick, ) -> Result<Vec<Option<C::Model>>, DbErr>
where O: ModelTrait<Entity: Morphable> + Sync, Db: ConnectionTrait,

For each owner, the child with the largest (Pick::Max) or smallest (Pick::Min) column, in one query: the latest comment of each post.

§Errors

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

Source

pub async fn latest_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, ) -> Result<Vec<Option<C::Model>>, DbErr>
where O: ModelTrait<Entity: Morphable> + Sync, Db: ConnectionTrait,

The latest child of each owner by column: pick_for with Pick::Max.

§Errors

As pick_for.

Source

pub async fn oldest_for<O, Db>( &self, db: &Db, owners: &[O], column: C::Column, ) -> Result<Vec<Option<C::Model>>, DbErr>
where O: ModelTrait<Entity: Morphable> + Sync, Db: ConnectionTrait,

The oldest child of each owner by column: pick_for with Pick::Min.

§Errors

As pick_for.

Source

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

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.

Source

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.

Source

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)"#
);
Source

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.

Source

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.

Source

pub fn owners_without<O: Morphable>(&self, children: Select<C>) -> Condition

For queries on O: owners without any child that children matches.

Trait Implementations§

Source§

impl<C: Clone + EntityTrait> Clone for Morph<C>
where C::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<C: Copy + EntityTrait> Copy for Morph<C>
where C::Column: Copy,

Source§

impl<C: Debug + EntityTrait> Debug for Morph<C>
where C::Column: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> Forget for Morph<C>

§

impl<C> Freeze for Morph<C>
where <C as EntityTrait>::Column: Freeze,

§

impl<C> Move for Morph<C>

§

impl<C> RefUnwindSafe for Morph<C>

§

impl<C> Send for Morph<C>
where <C as EntityTrait>::Column: Send,

§

impl<C> Sync for Morph<C>
where <C as EntityTrait>::Column: Sync,

§

impl<C> Unpin for Morph<C>
where <C as EntityTrait>::Column: Unpin,

§

impl<C> UnsafeUnpin for Morph<C>

§

impl<C> UnwindSafe for Morph<C>
where <C 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