Skip to main content

Crate sea_orm_relations_kit

Crate sea_orm_relations_kit 

Source
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): Morph for one-to-one and one-to-many, with the child resolving its owner through morph_target!; MorphToMany for many-to-many through a pivot; Morphable names 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 with Related<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: schema builds 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 MorphTarget for it.

Structs§

Changes
What a sync or toggle changed.
Morph
The *_type and *_id columns of a polymorphic child: commentable_type and commentable_id on comments.
Morphed
Marks a polymorphic many-to-many relation: MorphToMany.
PivotRelation
A many-to-many relation from owners to R through the pivot entity P.
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§

MorphTarget
The possible owners of a polymorphic child, as one enum.
Morphable
An entity that can own polymorphic children, with the name stored in their *_type column.
PivotOwner
An entity that can own rows of a pivot relation of kind K: any entity for Plain, a Morphable one for Morphed.

Functions§

aggregate_related
SUM, AVG, MIN or MAX of column over each owner’s related rows, as f64; None for an owner without related rows.
count_related
How many related rows each owner has: [3, 0, 1] for three posts.
count_related_with
count_related counting only the related rows related matches.
exists_related
Whether each owner has any related row.
latest_of_many
The latest related row of each owner by column: of_many with Pick::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_many with Pick::Min.

Type Aliases§

BelongsToMany
A plain many-to-many relation: users ↔ roles through role_user (user_id, role_id).
MorphToMany
A polymorphic many-to-many relation: posts and videos ↔ tags through taggables (tag_id, taggable_type, taggable_id).