#[derive(Entity)]
{
// Attributes available to this derive:
#[ormkit]
}
Expand description
Derive macro for implementing Entity trait
This macro automatically implements the Entity trait and generates
an accompanying ActiveModel struct for tracking changes.
§Attributes
§Struct-level attributes
#[ormkit(table = "table_name")]- Specify the database table name
§Field-level attributes
#[ormkit(id)]- Mark the primary key field (required)#[ormkit(belongs_to = "EntityName")]- Define foreign key relationship#[ormkit(column = "column_name")]- Override default column name
§Example
use ormkit::Entity;
use uuid::Uuid;
#[derive(ormkit::Entity)]
#[ormkit(table = "users")]
struct User {
#[ormkit(id)]
id: Uuid,
email: String,
name: String,
#[ormkit(belongs_to = "Organization")]
organization_id: Uuid,
}§Generated Code
The macro generates:
- Implementation of
Entitytrait - An
ActiveModelstruct withActiveValue<T>fields - Implementation of
ActiveModelTraitfor the generated ActiveModel - Helper methods for CRUD operations
§Constraints
- Exactly one field must be marked with
#[ormkit(id)] - The struct must be named (not a tuple struct or unit struct)
- Generic structs are not supported
- The ID field type must be one of: Uuid, i32, i64, String