Skip to main content

pg_enum

Attribute Macro pg_enum 

Source
#[pg_enum]
Expand description

Derive PostgreSQL enum <-> Rust enum mapping with FromSql and ToSql.

§Usage

#[bsql::pg_enum]
pub enum TicketStatus {
    #[sql("new")]
    New,
    #[sql("in_progress")]
    InProgress,
    #[sql("resolved")]
    Resolved,
    #[sql("closed")]
    Closed,
}

Each variant must have a #[sql("label")] attribute mapping it to the exact PostgreSQL enum label. The macro generates:

  • FromSql — deserializes from PostgreSQL text representation
  • ToSql — serializes to PostgreSQL text representation
  • Display — formats as the SQL label
  • Derives: Debug, Clone, Copy, PartialEq, Eq, Hash

If PostgreSQL sends a variant not present in the Rust enum, FromSql returns an error describing the schema mismatch.