Skip to main content

Table

Derive Macro Table 

Source
#[derive(Table)]
{
    // Attributes available to this derive:
    #[table]
    #[primary_key]
    #[column]
}
Expand description

Derive the Table trait for a struct.

§Attributes

§Struct-level: #[table(...)]

  • name = "table_name" - Database table name (defaults to snake_case of struct name)
  • schema = "schema_name" - Database schema (defaults to “public”)

§Field-level: #[primary_key] / #[primary_key(auto_generate)]

  • Marks a field as part of the primary key
  • auto_generate excludes it from inserts (e.g., serial/identity columns)

§Field-level: #[column(...)]

  • name = "col_name" - Database column name (defaults to field name)
  • skip - Skip this field entirely
  • auto_generate - Exclude from inserts (e.g., auto-populated columns)

§Example

#[derive(Table, sqlx::FromRow)]
#[table(name = "cities")]
struct City {
    #[primary_key(auto_generate)]
    pub id: i32,
    #[column(name = "name")]
    pub name: String,
    pub country_id: i32,
}