Skip to main content

TableSchema

Trait TableSchema 

Source
pub trait TableSchema
where Self: Encode + 'static,
{ type Record: TableRecord<Schema = Self>; type Insert: InsertRecord<Schema = Self>; type Update: UpdateRecord<Schema = Self>; type ForeignFetcher: ForeignFetcher; // Required methods fn table_name() -> &'static str; fn columns() -> &'static [ColumnDef]; fn primary_key() -> &'static str; fn to_values(self) -> Vec<(ColumnDef, Value)>; fn sanitizer(column_name: &'static str) -> Option<Box<dyn Sanitize>>; fn validator(column_name: &'static str) -> Option<Box<dyn Validate>>; // Provided methods fn indexes() -> &'static [IndexDef] { ... } fn foreign_fetcher() -> Self::ForeignFetcher { ... } fn schema_snapshot() -> TableSchemaSnapshot { ... } fn fingerprint() -> TableFingerprint { ... } }
Expand description

Table schema representation.

It is used to define the structure of a database table.

Required Associated Types§

Source

type Record: TableRecord<Schema = Self>

The TableRecord type associated with this table schema; which is the data returned by a query.

Source

type Insert: InsertRecord<Schema = Self>

The InsertRecord type associated with this table schema.

Source

type Update: UpdateRecord<Schema = Self>

The UpdateRecord type associated with this table schema.

Source

type ForeignFetcher: ForeignFetcher

The ForeignFetcher type associated with this table schema.

Required Methods§

Source

fn table_name() -> &'static str

Returns the name of the table.

Source

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

Returns the column definitions of the table.

Source

fn primary_key() -> &'static str

Returns the name of the primary key column.

Source

fn to_values(self) -> Vec<(ColumnDef, Value)>

Converts itself into a vector of column-value pairs.

Source

fn sanitizer(column_name: &'static str) -> Option<Box<dyn Sanitize>>

Returns the Sanitize implementation for the given column name, if any.

Source

fn validator(column_name: &'static str) -> Option<Box<dyn Validate>>

Returns the Validate implementation for the given column name, if any.

Provided Methods§

Source

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

Returns the list of indexes defined on the table, where each index is represented by the list of column names it includes.

Source

fn foreign_fetcher() -> Self::ForeignFetcher

Returns an instance of the ForeignFetcher for this table schema.

Source

fn schema_snapshot() -> TableSchemaSnapshot

Builds a self-describing TableSchemaSnapshot from the compile-time schema definition.

The snapshot captures the structural shape of the table — name, primary key, alignment, columns and indexes — in a stable, encodable form so it can be persisted to stable memory and later diffed against the snapshot of a previous version to derive the migration steps required to bring the on-disk layout up to date.

The default implementation assembles the snapshot from Self::table_name, Self::primary_key, Self::columns, Self::indexes and the Encode::ALIGNMENT constant. It is sufficient for every schema generated by #[derive(Table)]; implementors that carry metadata not exposed through ColumnDef (e.g. column defaults or non-default foreign-key ON DELETE actions) should override it.

Source

fn fingerprint() -> TableFingerprint

Returns the fingerprint of the table schema.

The fingerprint is computed as a hash of Self::table_name so that the same table keeps the same identity across rebuilds (where std::any::TypeId is not stable) and across schema evolution. Two distinct types declaring the same table_name are intentionally considered the same logical table.

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§