#[derive(Model)]
{
// Attributes available to this derive:
#[sqlmodel]
}
Expand description
Derive macro for the Model trait.
This macro generates implementations for:
- Table name and primary key metadata
- Field information
- Row conversion (to_row, from_row)
- Primary key access
§Attributes
#[sqlmodel(table = "name")]- Override table name (defaults to snake_case struct name)#[sqlmodel(primary_key)]- Mark field as primary key#[sqlmodel(auto_increment)]- Mark field as auto-incrementing#[sqlmodel(column = "name")]- Override column name#[sqlmodel(nullable)]- Mark field as nullable#[sqlmodel(unique)]- Add unique constraint#[sqlmodel(default = "expr")]- Set default SQL expression#[sqlmodel(foreign_key = "table.column")]- Add foreign key reference#[sqlmodel(index = "name")]- Add to named index#[sqlmodel(skip)]- Skip this field in database operations
§Example
ⓘ
use sqlmodel::Model;
#[derive(Model)]
#[sqlmodel(table = "heroes")]
struct Hero {
#[sqlmodel(primary_key, auto_increment)]
id: Option<i64>,
#[sqlmodel(unique)]
name: String,
secret_name: String,
#[sqlmodel(nullable)]
age: Option<i32>,
#[sqlmodel(foreign_key = "teams.id")]
team_id: Option<i64>,
}