Skip to main content

table

Attribute Macro table 

Source
#[table]
Expand description

Mark a struct as a SQL table.

§Example

#[reef::table(strict)]
#[index(name = "users_email_idx", columns = ["email"])]
pub struct User {
    #[column(primary_key, auto_increment)]
    pub id: i64,
    #[column(unique)]
    pub email: String,
    pub name: String,
}

#[reef::table]
#[primary_key(columns = ["user_id", "post_id"])]
pub struct PostLike {
    #[column(references = "users(id)", on_delete = "cascade")]
    pub user_id: i64,
    #[column(references = "posts(id)", on_delete = "cascade")]
    pub post_id: i64,
}

§Macro arguments — #[reef::table(...)]

  • name = "<sql_table_name>" — override the default snake_case derivation
  • strict — emit as a SQLite STRICT table (3.37+)
  • without_rowid — emit as a WITHOUT ROWID table (perf optimization for tables with non-INTEGER primary keys)

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

  • primary_key — single-column PK (use #[primary_key(columns = [...])] at the struct level for composite PKs)
  • auto_increment — emit AUTOINCREMENT (only valid with INTEGER PK)
  • unique
  • default = <expr> — Rust literal. String literals get SQL-quoted (default = "active"DEFAULT 'active'); numerics/bools emit raw.
  • default_sql = "<sql>" — verbatim SQL passthrough for function calls like default_sql = "datetime('now')" (use this when you need DEFAULT (datetime('now')) rather than a quoted string literal).
  • check = <expr> — SQL CHECK constraint scoped to this column
  • references = "<table>(<column>)" — single-column FK target
  • on_delete = "cascade" | "restrict" | "set_null" | "set_default" | "no_action"
  • on_update = "..." — same options as on_delete
  • generated = "<sql_expr>"GENERATED ALWAYS AS (<expr>)
  • generated_kind = "stored" | "virtual" — defaults to “virtual”

§Struct-level — helper attributes

  • #[index(name = "...", columns = [...], unique)] — single or multi-column index
  • #[primary_key(columns = [...])] — composite primary key
  • #[foreign_key(columns = [...], references = "<table>(<col>, <col>, ...)", on_delete = "...", on_update = "...")] — composite foreign key
  • #[check(name = "...", expr = "...")] — named table-level CHECK