Skip to main content

EntityTrait

Trait EntityTrait 

Source
pub trait EntityTrait: Sized {
    // Required methods
    fn table() -> &'static str;
    fn columns() -> &'static [ColumnSpec];
    fn primary_key() -> &'static str;
    fn from_row(row: &dyn Row) -> Result<Self>;
    fn insert_values(&self) -> Vec<(&'static str, Value)>;
    fn update_values(&self) -> Vec<(&'static str, Value)>;
    fn pk_value(&self) -> Value;

    // Provided methods
    fn indexes() -> &'static [IndexSpec] { ... }
    fn foreign_keys() -> &'static [ForeignKeySpec] { ... }
    fn shard_columns() -> &'static [&'static str] { ... }
    fn value_of(&self, column: &str) -> Option<Value> { ... }
}
Expand description

A struct that maps to one table. Implemented by #[derive(Entity)]; hand impls are possible but the derive is the intended path.

Required Methods§

Source

fn table() -> &'static str

The table name.

Source

fn columns() -> &'static [ColumnSpec]

Column metadata, in declaration order. Drives both DDL generation and the column list of generated INSERTs.

Source

fn primary_key() -> &'static str

The primary-key column name. Used by find_by_pk / update / delete_by_pk.

Source

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

Decode one row into Self. Generated to read each field through its FromColumn impl.

Source

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

(column, value) pairs for an INSERT. Auto-increment primary keys are omitted so the database assigns them.

Source

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

(column, value) pairs for an UPDATE — every column except the primary key.

Source

fn pk_value(&self) -> Value

This instance’s primary-key value, for WHERE pk = ?.

Provided Methods§

Source

fn indexes() -> &'static [IndexSpec]

Secondary indexes (single-column and composite, unique or not), emitted as separate CREATE INDEX statements by crate::schema. Defaults to none.

Source

fn foreign_keys() -> &'static [ForeignKeySpec]

Foreign-key constraints, emitted inline in the CREATE TABLE. These enforce referential integrity within one database; they are not ORM relationships (no navigation is generated). Defaults to none.

Source

fn shard_columns() -> &'static [&'static str]

Columns whose values are the shard key — a shard-encoded id (#[orm(shard_key)]), the “user id by proxy”. When a query constrains one of these, the repo layer routes the operation to that id’s shard.

This is the only per-entity sharding metadata, and it’s unavoidable: the key column differs per table (id on the owner, user_id on its rows), and its presence is what marks an entity sharded vs global. Everything else about sharding — the family name, the id codec, whether a connector shards at all — lives on the connector, not here. An entity with no shard-key column is global, and sharding never affects it.

Source

fn value_of(&self, column: &str) -> Option<Value>

One column’s value by name, or None if the entity has no such column.

What relationship loading needs: given a slice of parents, collect the key column to look children up by. Defaults to a scan of update_values plus the primary key, which is correct for every entity and costs one allocation of the row’s values.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§