pub trait Model:
Sized
+ Send
+ Sync {
const TABLE_NAME: &'static str;
const PRIMARY_KEY: &'static [&'static str];
const RELATIONSHIPS: &'static [RelationshipInfo] = _;
const SHARD_KEY: Option<&'static str> = None;
// Required methods
fn fields() -> &'static [FieldInfo];
fn to_row(&self) -> Vec<(&'static str, Value)>;
fn from_row(row: &Row) -> Result<Self>;
fn primary_key_value(&self) -> Vec<Value>;
fn is_new(&self) -> bool;
// Provided methods
fn inheritance() -> InheritanceInfo { ... }
fn model_config() -> ModelConfig { ... }
fn shard_key_value(&self) -> Option<Value> { ... }
}Expand description
Trait for types that can be mapped to database tables.
This trait provides metadata about the table structure and methods for converting between Rust structs and database rows.
§Example
use sqlmodel::Model;
#[derive(Model)]
#[sqlmodel(table = "heroes")]
struct Hero {
#[sqlmodel(primary_key)]
id: Option<i64>,
name: String,
secret_name: String,
age: Option<i32>,
}Required Associated Constants§
Sourceconst TABLE_NAME: &'static str
const TABLE_NAME: &'static str
The name of the database table.
Sourceconst PRIMARY_KEY: &'static [&'static str]
const PRIMARY_KEY: &'static [&'static str]
The primary key column name(s).
Provided Associated Constants§
Sourceconst RELATIONSHIPS: &'static [RelationshipInfo] = _
const RELATIONSHIPS: &'static [RelationshipInfo] = _
Relationship metadata for this model.
The derive macro will populate this for relationship fields; models with no relationships can rely on the default empty slice.
Required Methods§
Sourcefn primary_key_value(&self) -> Vec<Value>
fn primary_key_value(&self) -> Vec<Value>
Get the value of the primary key field(s).
Provided Methods§
Sourcefn inheritance() -> InheritanceInfo
fn inheritance() -> InheritanceInfo
Inheritance metadata for this model.
Returns information about table inheritance if this model participates in an inheritance hierarchy (single, joined, or concrete table). The default implementation returns no inheritance.
Sourcefn model_config() -> ModelConfig
fn model_config() -> ModelConfig
Get the model configuration.
Returns model-level configuration that affects validation, serialization, and database operations.
Sourcefn shard_key_value(&self) -> Option<Value>
fn shard_key_value(&self) -> Option<Value>
Get the shard key value for this model instance.
Returns None if the model doesn’t have a shard key defined.
The returned value is used by ShardedPool to determine the
appropriate shard for insert/update/delete operations.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.