Expand description
Tags for any SeaORM entity.
One tags table serves every taggable entity through the polymorphic taggables
pivot. Mark an entity, and each of its rows gets a tags handle:
use sea_orm::{DatabaseConnection, DbErr, EntityTrait, QueryFilter};
use sea_orm_taggable::{HasTags, Morphable, TagFilters, Taggable};
impl Morphable for posts::Entity {
const MORPH_TYPE: &'static str = "post";
}
impl Taggable for posts::Entity {}
async fn example(db: &DatabaseConnection, post: &posts::Model) -> Result<(), DbErr> {
post.tags().attach(db, ["Rust", "SeaORM"], None).await?;
post.tags().attach(db, ["Beginner"], Some("level")).await?;
let about_rust = posts::Entity::find()
.filter(posts::Entity::with_any_tags(["rust"], None))
.all(db)
.await?;
Ok(())
}- Tags are created from names on first use; a slug is made from the name, unique within the tag’s type.
- Types group tags (
"topic","level");Noneis the plain group. - Each row keeps its tags in its own order; tags also have a global position within
their type (
tag::Model::move_to_startand friends). - Load the tags of a page of rows in two queries with
load_tags.
The pivot cannot have a foreign key to the tagged rows, so call
Tags::detach_all in the row’s delete hook (for soft-deleted rows, when they
are deleted for good), or delete_orphans from a scheduled task.
Modules§
- schema
- The migration for the
tagsandtaggablestables. - tag
- The
tagstable: a tag, its slug, an optional type and its position in the list. - taggable
- The
taggablespivot table: which tag is on which row, and in which position.
Structs§
- Changes
- What a sync or toggle changed.
- Tags
- The tags of one row, from
HasTags::tags.
Enums§
Constants§
- TAGS
- The polymorphic many-to-many relation behind the tags, for queries this crate
does not cover; see
sea-orm-relations-kit.
Traits§
- HasTags
- Gives every model of a
Taggableentity atagshandle. - Morphable
- An entity that can own polymorphic children, with the name stored in their
*_typecolumn. - TagFilters
- Filters on a
Taggableentity’s tags, as conditions for.filter(...). - Taggable
- An entity whose rows can be tagged. Its
Morphable::MORPH_TYPEis stored intaggables.taggable_type, and its primary key must be an integer.
Functions§
- count_
tags - How many tags each row has, in one query.
- delete_
orphans - Removes the pivot rows of
Orows that no longer exist. Returns how many went. - load_
tags - The tags of many rows, in two queries, each list in the row’s own order.