Skip to main content

Model

Trait Model 

Source
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§

Source

const TABLE_NAME: &'static str

The name of the database table.

Source

const PRIMARY_KEY: &'static [&'static str]

The primary key column name(s).

Provided Associated Constants§

Source

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.

Source

const SHARD_KEY: Option<&'static str> = None

The shard key field name for horizontal sharding.

Returns None if the model doesn’t use sharding. When set, the sharded pool will use this field’s value to determine which shard to route queries to.

Required Methods§

Source

fn fields() -> &'static [FieldInfo]

Get field metadata for all columns.

Source

fn to_row(&self) -> Vec<(&'static str, Value)>

Convert this model instance to a row of values.

Source

fn from_row(row: &Row) -> Result<Self>

Construct a model instance from a database row.

Source

fn primary_key_value(&self) -> Vec<Value>

Get the value of the primary key field(s).

Source

fn is_new(&self) -> bool

Check if this is a new record (primary key is None/default).

Provided Methods§

Source

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.

Source

fn model_config() -> ModelConfig

Get the model configuration.

Returns model-level configuration that affects validation, serialization, and database operations.

Source

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.

Implementors§