Skip to main content

Model

Derive Macro Model 

Source
#[derive(Model)]
{
    // Attributes available to this derive:
    #[orm]
}
Expand description

Derive Model metadata for a struct.

§Example

use pgorm::Model;

#[derive(Model)]
#[orm(table = "users")]
struct User {
    #[orm(id)]
    user_id: i64,
    username: String,
    email: Option<String>,
}

§Generated

  • TABLE: &'static str - Table name
  • COL_*: &'static str - Column name constants
  • SELECT_LIST: &'static str - Comma-separated column list
  • fn select_list_as(alias: &str) -> String - Aliased column list for JOINs

§Attributes

Struct-level:

  • #[orm(table = "name")] - Specify table name (required)
  • #[orm(join(table = "...", on = "...", type = "inner|left|right|full|cross"))] - Add JOINs (optional, repeatable)
  • #[orm(has_many(ChildType, foreign_key = "...", as = "..."))] - Generate select_has_many helpers (optional, repeatable)
  • #[orm(belongs_to(ParentType, foreign_key = "...", as = "..."))] - Generate select_belongs_to helpers (optional, repeatable)

Field-level:

  • #[orm(id)] - Mark field as primary key
  • #[orm(column = "name")] - Map field to a different column name
  • #[orm(table = "name")] - Mark field as coming from a joined table (for view/join models)