#[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.
§Struct-level options (applied as a second #[entity(...)] attribute)
timestamps— injectscreated_atandupdated_atcolumns of typeDateTime<Utc>and sets them automatically inbefore_save.soft_delete— injects adeleted_at: Option<DateTime<Utc>>column and generatesfind,find_by_id,with_deleted,only_deleted,soft_delete,restore, andforce_deletehelpers on the entity module.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"|"nanoid"— generates a ULID or NanoID 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.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).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.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,
}