#[entity]Expand description
Attribute macro for declaring a SeaORM database entity with auto-registration.
The macro wraps the annotated struct in a SeaORM entity module and submits an
EntityRegistration to the inventory collector so modo_db::sync_and_migrate
can discover it at startup.
§Required argument
table = "<name>"— SQL table name.
§Optional argument
group = "<name>"— assigns the entity to a named group (default:"default"). Entities in a group can be synced separately viamodo_db::sync_and_migrate_group.
§Struct-level options (applied as a second #[entity(...)] attribute)
timestamps— injectscreated_atandupdated_atcolumns of typeDateTime<Utc>; both are set automatically on every insert and update.soft_delete— injects adeleted_at: Option<DateTime<Utc>>column. Thedeletemethod becomes a soft-delete (setsdeleted_at). Extra methods generated on the struct:with_deleted,only_deleted,restore,force_delete,force_delete_by_id,delete_many(bulk soft-delete),force_delete_many(bulk hard-delete).find_allandqueryare overridden to exclude soft-deleted rows automatically.framework— marks the entity as framework-internal (non-user schema).index(columns = ["col1", "col2"])— creates a composite index. Adduniqueinside to make it a unique index.
§Field-level options (applied as #[entity(...)] on individual fields)
primary_key— marks the field as the primary key.auto_increment = true|false— overrides SeaORM’s default auto-increment behaviour.auto = "ulid"|"short_id"— generates a ULID or short ID before insert; only valid onprimary_keyfields.unique— adds a unique constraint.indexed— creates a single-column index.nullable— accepted but has no effect (SeaORM infers nullability fromOption<T>).column_type = "<type>"— overrides the inferred SeaORM column type string.default_value = <literal>— sets a default value (passed to SeaORM).default_expr = "<expr>"— sets a default SQL expression string.belongs_to = "<Entity>"— declares aBelongsTorelation to the named entity. Pair withon_delete/on_updateas needed.to_column = "<Column>"— overrides the target column for abelongs_toFK (default:"Id").on_delete = "<action>"— FK action on delete. One of:Cascade,SetNull,Restrict,NoAction,SetDefault.on_update = "<action>"— FK action on update. Same values ason_delete.has_many— declares aHasManyrelation (field is excluded from the model). Requirestarget = "<Entity>".has_one— declares aHasOnerelation (field is excluded from the model).via = "<JoinEntity>"— used withhas_many/has_onefor many-to-many relations through a join entity.target = "<Entity>"— required forhas_many; overrides the inferred target entity name forhas_onewhen the field name does not match the entity name.renamed_from = "<old_name>"— records a rename hint as a column comment.
§Example
ⓘ
#[modo_db::entity(table = "users")]
#[entity(timestamps, soft_delete)]
pub struct User {
#[entity(primary_key, auto = "ulid")]
pub id: String,
#[entity(unique)]
pub email: String,
pub name: String,
}