Skip to main content

FieldInfo

Struct FieldInfo 

Source
pub struct FieldInfo {
Show 31 fields pub name: &'static str, pub column_name: &'static str, pub sql_type: SqlType, pub sql_type_override: Option<&'static str>, pub precision: Option<u8>, pub scale: Option<u8>, pub nullable: bool, pub primary_key: bool, pub auto_increment: bool, pub unique: bool, pub default: Option<&'static str>, pub foreign_key: Option<&'static str>, pub on_delete: Option<ReferentialAction>, pub on_update: Option<ReferentialAction>, pub index: Option<&'static str>, pub alias: Option<&'static str>, pub validation_alias: Option<&'static str>, pub serialization_alias: Option<&'static str>, pub computed: bool, pub exclude: bool, pub title: Option<&'static str>, pub description: Option<&'static str>, pub schema_extra: Option<&'static str>, pub default_json: Option<&'static str>, pub has_default: bool, pub const_field: bool, pub column_constraints: &'static [&'static str], pub column_comment: Option<&'static str>, pub column_info: Option<&'static str>, pub hybrid_sql: Option<&'static str>, pub discriminator: Option<&'static str>,
}
Expand description

Metadata about a model field/column.

Fields§

§name: &'static str

Rust field name

§column_name: &'static str

Database column name (may differ from field name)

§sql_type: SqlType

SQL type for this field

§sql_type_override: Option<&'static str>

Explicit SQL type override string (e.g., “VARCHAR(255)”, “DECIMAL(10,2)”) When set, this takes precedence over sql_type in DDL generation.

§precision: Option<u8>

Precision for DECIMAL/NUMERIC types (total digits)

§scale: Option<u8>

Scale for DECIMAL/NUMERIC types (digits after decimal point)

§nullable: bool

Whether this field is nullable

§primary_key: bool

Whether this is a primary key

§auto_increment: bool

Whether this field auto-increments

§unique: bool

Whether this field has a unique constraint

§default: Option<&'static str>

Default value expression (SQL)

§foreign_key: Option<&'static str>

Foreign key reference (table.column)

§on_delete: Option<ReferentialAction>

Referential action for ON DELETE (only valid with foreign_key)

§on_update: Option<ReferentialAction>

Referential action for ON UPDATE (only valid with foreign_key)

§index: Option<&'static str>

Index name if indexed

§alias: Option<&'static str>

Alias for both input and output (like serde rename). When set, this name is used instead of name for serialization/deserialization.

§validation_alias: Option<&'static str>

Alias used only during deserialization/validation (input-only). Accepts this name as an alternative to name or alias during parsing.

§serialization_alias: Option<&'static str>

Alias used only during serialization (output-only). Overrides alias when outputting the field name.

§computed: bool

Whether this is a computed field (not stored in database). Computed fields are excluded from database operations but included in serialization (model_dump) unless exclude_computed_fields is set.

§exclude: bool

Whether to exclude this field from serialization (model_dump). When true, the field will never appear in serialized output.

§title: Option<&'static str>

Schema title for JSON Schema generation. Used as the “title” property in the generated JSON Schema.

§description: Option<&'static str>

Schema description for JSON Schema generation. Used as the “description” property in the generated JSON Schema.

§schema_extra: Option<&'static str>

Extra JSON Schema properties (as JSON string, merged into schema). The string should be valid JSON that will be merged into the field’s schema.

§default_json: Option<&'static str>

JSON representation of the field’s default value (for exclude_defaults). When set, model_dump with exclude_defaults=true will compare the current value against this and exclude the field if they match.

§has_default: bool

Whether this field has a default value (used for exclude_unset tracking). Fields with defaults can be distinguished from fields that were explicitly set.

§const_field: bool

Whether this field is constant (immutable after creation). Const fields cannot be modified after initial construction. This is enforced at validation/session level, not compile-time.

§column_constraints: &'static [&'static str]

Additional SQL constraints for DDL generation (e.g., CHECK constraints). Each string is a SQL constraint expression that will be added to the column definition.

§column_comment: Option<&'static str>

SQL comment for the column (used in DDL generation). This maps to the COMMENT ON COLUMN or inline COMMENT clause depending on the database.

§column_info: Option<&'static str>

Extra metadata as JSON string (for custom extensions/info). This can be used to store additional information that doesn’t fit in other fields.

§hybrid_sql: Option<&'static str>

SQL expression for hybrid properties.

When set, this field is a hybrid property: it has both a Rust-side computed value and a SQL expression that can be used in queries (WHERE, ORDER BY, etc.). The macro generates a {field}_expr() associated function that returns Expr::raw(this_sql).

§discriminator: Option<&'static str>

Discriminator field name for union types.

When this field contains a union/enum type, the discriminator specifies which field within the union variants is used to determine the type. This maps to Pydantic’s Field(discriminator='field_name').

In Rust, discriminated unions are typically handled by serde’s #[serde(tag = "field_name")] attribute on the enum. This field stores the discriminator info for:

  • JSON Schema generation (OpenAPI discriminator)
  • Documentation purposes
  • Runtime validation hints

Example:

#[derive(Model)]
struct Owner {
    #[sqlmodel(discriminator = "pet_type")]
    pet: PetUnion, // PetUnion should have #[serde(tag = "pet_type")]
}

Implementations§

Source§

impl FieldInfo

Source

pub const fn new( name: &'static str, column_name: &'static str, sql_type: SqlType, ) -> Self

Create a new field info with minimal required data.

Source

pub const fn column(self, name: &'static str) -> Self

Set the database column name.

Source

pub const fn sql_type_override(self, type_str: &'static str) -> Self

Set explicit SQL type override.

When set, this string will be used directly in DDL generation instead of the sql_type.sql_name(). Use this for database-specific types like VARCHAR(255), DECIMAL(10,2), TINYINT UNSIGNED, etc.

Source

pub const fn sql_type_override_opt(self, type_str: Option<&'static str>) -> Self

Set SQL type override from optional.

Source

pub const fn precision(self, value: u8) -> Self

Set precision for DECIMAL/NUMERIC types.

Precision is the total number of digits (before and after decimal point). Typical range: 1-38, depends on database.

§Example
// DECIMAL(10, 2) - 10 total digits, 2 after decimal
FieldInfo::new("price", "price", SqlType::Decimal { precision: 10, scale: 2 })
    .precision(10)
    .scale(2)
Source

pub const fn precision_opt(self, value: Option<u8>) -> Self

Set precision from optional.

Source

pub const fn scale(self, value: u8) -> Self

Set scale for DECIMAL/NUMERIC types.

Scale is the number of digits after the decimal point. Must be less than or equal to precision.

Source

pub const fn scale_opt(self, value: Option<u8>) -> Self

Set scale from optional.

Source

pub const fn decimal_precision(self, precision: u8, scale: u8) -> Self

Set both precision and scale for DECIMAL/NUMERIC types.

§Example
// DECIMAL(10, 2) for currency
FieldInfo::new("price", "price", SqlType::Decimal { precision: 10, scale: 2 })
    .decimal_precision(10, 2)
Source

pub fn effective_sql_type(&self) -> String

Get the effective SQL type name for DDL generation.

Priority:

  1. sql_type_override if set
  2. For DECIMAL/NUMERIC: uses precision and scale fields if set
  3. Falls back to sql_type.sql_name()
Source

pub const fn nullable(self, value: bool) -> Self

Set nullable flag.

Source

pub const fn primary_key(self, value: bool) -> Self

Set primary key flag.

Source

pub const fn auto_increment(self, value: bool) -> Self

Set auto-increment flag.

Source

pub const fn unique(self, value: bool) -> Self

Set unique flag.

Source

pub const fn default(self, expr: &'static str) -> Self

Set default value.

Source

pub const fn default_opt(self, expr: Option<&'static str>) -> Self

Set default value from optional.

Source

pub const fn foreign_key(self, reference: &'static str) -> Self

Set foreign key reference.

Source

pub const fn foreign_key_opt(self, reference: Option<&'static str>) -> Self

Set foreign key reference from optional.

Source

pub const fn on_delete(self, action: ReferentialAction) -> Self

Set ON DELETE action for foreign key.

This is only meaningful when foreign_key is also set.

Source

pub const fn on_delete_opt(self, action: Option<ReferentialAction>) -> Self

Set ON DELETE action from optional.

Source

pub const fn on_update(self, action: ReferentialAction) -> Self

Set ON UPDATE action for foreign key.

This is only meaningful when foreign_key is also set.

Source

pub const fn on_update_opt(self, action: Option<ReferentialAction>) -> Self

Set ON UPDATE action from optional.

Source

pub const fn index(self, name: &'static str) -> Self

Set index name.

Source

pub const fn index_opt(self, name: Option<&'static str>) -> Self

Set index name from optional.

Source

pub const fn alias(self, name: &'static str) -> Self

Set alias for both input and output.

When set, this name is used instead of the field name for both serialization and deserialization.

Source

pub const fn alias_opt(self, name: Option<&'static str>) -> Self

Set alias from optional.

Source

pub const fn validation_alias(self, name: &'static str) -> Self

Set validation alias (input-only).

This name is accepted as an alternative during deserialization, in addition to the field name and regular alias.

Source

pub const fn validation_alias_opt(self, name: Option<&'static str>) -> Self

Set validation alias from optional.

Source

pub const fn serialization_alias(self, name: &'static str) -> Self

Set serialization alias (output-only).

This name is used instead of the field name or regular alias when serializing the field.

Source

pub const fn serialization_alias_opt(self, name: Option<&'static str>) -> Self

Set serialization alias from optional.

Source

pub const fn computed(self, value: bool) -> Self

Mark this field as computed (not stored in database).

Computed fields are:

  • Excluded from database operations (INSERT, UPDATE, SELECT)
  • Initialized with Default::default() when loading from database
  • Included in serialization (model_dump) unless exclude_computed_fields is set

Use this for fields whose value is derived from other fields at access time.

Source

pub const fn exclude(self, value: bool) -> Self

Mark this field as excluded from serialization (model_dump).

Excluded fields will never appear in serialized output, regardless of other serialization settings. This is useful for sensitive data like passwords or internal fields.

§Example
#[derive(Model)]
struct User {
    id: i32,
    #[sqlmodel(exclude)]
    password_hash: String,  // Never serialized
}
Source

pub const fn title(self, value: &'static str) -> Self

Set the schema title for JSON Schema generation.

The title appears as the “title” property in the field’s JSON Schema.

Source

pub const fn title_opt(self, value: Option<&'static str>) -> Self

Set the schema title from optional.

Source

pub const fn description(self, value: &'static str) -> Self

Set the schema description for JSON Schema generation.

The description appears as the “description” property in the field’s JSON Schema.

Source

pub const fn description_opt(self, value: Option<&'static str>) -> Self

Set the schema description from optional.

Source

pub const fn schema_extra(self, value: &'static str) -> Self

Set extra JSON Schema properties (as JSON string).

The string should be valid JSON that will be merged into the field’s schema. For example: {"examples": ["John Doe"], "minLength": 1}

Source

pub const fn schema_extra_opt(self, value: Option<&'static str>) -> Self

Set schema_extra from optional.

Source

pub const fn default_json(self, value: &'static str) -> Self

Set the JSON representation of the field’s default value.

This is used by model_dump with exclude_defaults=true to compare the current value against the default.

§Example
FieldInfo::new("count", "count", SqlType::Integer)
    .default_json("0")
    .has_default(true)
Source

pub const fn default_json_opt(self, value: Option<&'static str>) -> Self

Set default_json from optional.

Source

pub const fn has_default(self, value: bool) -> Self

Mark whether this field has a default value.

Used for exclude_unset tracking - fields with defaults may not need to be explicitly set.

Source

pub const fn const_field(self, value: bool) -> Self

Mark this field as constant (immutable after creation).

Const fields cannot be modified after the model is initially created. This is useful for version numbers, creation timestamps, or other immutable identifiers.

§Example
FieldInfo::new("version", "version", SqlType::Text)
    .const_field(true)
Source

pub const fn column_constraints( self, constraints: &'static [&'static str], ) -> Self

Set additional SQL constraints for DDL generation.

These constraints are added to the column definition. Common uses include CHECK constraints.

§Example
FieldInfo::new("age", "age", SqlType::Integer)
    .column_constraints(&["CHECK(age >= 0)", "CHECK(age <= 150)"])
Source

pub const fn column_comment(self, comment: &'static str) -> Self

Set a SQL comment for the column.

The comment will be included in DDL generation.

Source

pub const fn column_comment_opt(self, comment: Option<&'static str>) -> Self

Set column comment from optional.

Source

pub const fn column_info(self, info: &'static str) -> Self

Set extra metadata as JSON string.

This can be used for custom extensions or information that doesn’t fit in other fields.

Source

pub const fn column_info_opt(self, info: Option<&'static str>) -> Self

Set column info from optional.

Source

pub const fn hybrid_sql(self, sql: &'static str) -> Self

Set hybrid SQL expression.

Source

pub const fn hybrid_sql_opt(self, sql: Option<&'static str>) -> Self

Set hybrid SQL expression from optional.

Source

pub const fn discriminator(self, field: &'static str) -> Self

Set discriminator field name for union types.

This specifies which field in the union variants is used to determine the concrete type during deserialization. The union type should have a matching #[serde(tag = "discriminator_field")] attribute.

Source

pub const fn discriminator_opt(self, field: Option<&'static str>) -> Self

Set discriminator from optional.

Source

pub const fn output_name(&self) -> &'static str

Get the name to use when serializing (output).

Priority: serialization_alias > alias > name

Source

pub fn matches_input_name(&self, input: &str) -> bool

Check if a given name matches this field for input (deserialization).

Matches: name, alias, or validation_alias

Source

pub const fn has_alias(&self) -> bool

Check if this field has any alias configuration.

Trait Implementations§

Source§

impl Clone for FieldInfo

Source§

fn clone(&self) -> FieldInfo

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FieldInfo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more