Skip to main content

Entity

Derive Macro Entity 

Source
#[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:

  1. Implementation of Entity trait
  2. An ActiveModel struct with ActiveValue<T> fields
  3. Implementation of ActiveModelTrait for the generated ActiveModel
  4. 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