Attribute Macro butane::model

source ·
#[model]
Expand description

Attribute macro which marks a struct as being a data model and generates an implementation of DataObject. This macro will also write information to disk at compile time necessary to generate migrations

Restrictions on model types:

  1. The type of each field must implement FieldType or be Many.
  2. There must be a primary key field. This must be either annotated with a #[pk] attribute or named id.

Helper Attributes

  • #[table = "NAME"] used on the struct to specify the name of the table (defaults to struct name)
  • #[pk] on a field to specify that it is the primary key.
  • #[auto] on a field indicates that the field’s value is initialized based on serial/auto-increment. Currently supported only on the primary key and only if the primary key is an integer type
  • #[unique] on a field indicates that the field’s value must be unique (perhaps implemented as the SQL UNIQUE constraint by some backends).
  • [default] should be used on fields added by later migrations to avoid errors on existing objects. Unnecessary if the new field is an Option<>

For example

#[model]
#[table = "posts"]
pub struct Post {
  #[auto]
  #[pk] // unnecessary if identifier were named id instead
  pub identifier: i32,
  pub title: String,
  pub content: String,
  #[default = false]
  pub published: bool,
}