Expand description
Relations for SeaORM that it does not have out of the box, with the operations that make them practical.
- Polymorphic relations, where a child row points at an owner that can be one of
several entities (
commentable_type+commentable_id):Morphfor one-to-one and one-to-many, with the child resolving its owner throughmorph_target!;MorphToManyfor many-to-many through a pivot;Morphablenames each owner entity. - Pivot operations for polymorphic and plain (
BelongsToMany) many-to-many:attach,detach,sync,sync_without_detaching,toggle,update_pivot, ordered pivots and extra pivot columns. - Eager loading and counts without N+1:
load_for,count_for. - For
SeaORM’s own relations (anything withRelated<R>):count_related,exists_related,aggregate_related(sum, average, min, max) and one row per owner by a column (latest_of_many,oldest_of_many,of_many). - Filters as conditions for any query: owners that have (or lack) matching related rows, children whose owner matches a query, owners with all given keys.
- Migrations:
schemabuilds the polymorphic columns and their index.
Batch methods put the owners’ keys in one IN (...) list; pass a page of rows,
not a whole table (Postgres allows 65 535 bind parameters per query).
Polymorphic columns cannot have a foreign key, so the database cannot clean up
after a deleted owner. Call delete_for / detach_all in the owner’s delete hook,
or delete_orphans from a scheduled task.
Modules§
- schema
- Migration helpers for polymorphic columns.
Macros§
- morph_
target - Declares the possible owners of a polymorphic child as an enum, and implements
MorphTargetfor it.
Structs§
- Changes
- What a sync or toggle changed.
- Morph
- The
*_typeand*_idcolumns of a polymorphic child:commentable_typeandcommentable_idoncomments. - Morphed
- Marks a polymorphic many-to-many relation:
MorphToMany. - Pivot
Relation - A many-to-many relation from owners to
Rthrough the pivot entityP. - Plain
- Marks a plain many-to-many relation:
BelongsToMany.
Enums§
- Aggregate
- An aggregate over a column of the related rows.
- Key
- A primary key value, normalized.
- Pick
- Which end of the column to pick.
Traits§
- Morph
Target - The possible owners of a polymorphic child, as one enum.
- Morphable
- An entity that can own polymorphic children, with the name stored in their
*_typecolumn. - Pivot
Owner - An entity that can own rows of a pivot relation of kind
K: any entity forPlain, aMorphableone forMorphed.
Functions§
- aggregate_
related SUM,AVG,MINorMAXofcolumnover each owner’s related rows, asf64;Nonefor an owner without related rows.- count_
related - How many related rows each owner has:
[3, 0, 1]for three posts. - count_
related_ with count_relatedcounting only the related rowsrelatedmatches.- exists_
related - Whether each owner has any related row.
- latest_
of_ many - The latest related row of each owner by
column:of_manywithPick::Max. - of_many
- For each owner, the related row with the largest (
Pick::Max) or smallest (Pick::Min)column, in one query. Only for direct relations (has_many). - oldest_
of_ many - The oldest related row of each owner by
column:of_manywithPick::Min.
Type Aliases§
- Belongs
ToMany - A plain many-to-many relation:
users ↔ rolesthroughrole_user (user_id, role_id). - Morph
ToMany - A polymorphic many-to-many relation: posts and videos ↔ tags through
taggables (tag_id, taggable_type, taggable_id).