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 strRust field name
column_name: &'static strDatabase column name (may differ from field name)
sql_type: SqlTypeSQL 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: boolWhether this field is nullable
primary_key: boolWhether this is a primary key
auto_increment: boolWhether this field auto-increments
unique: boolWhether 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: boolWhether 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: boolWhether 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: boolWhether 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: boolWhether 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
impl FieldInfo
Sourcepub const fn new(
name: &'static str,
column_name: &'static str,
sql_type: SqlType,
) -> Self
pub const fn new( name: &'static str, column_name: &'static str, sql_type: SqlType, ) -> Self
Create a new field info with minimal required data.
Sourcepub const fn sql_type_override(self, type_str: &'static str) -> Self
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.
Sourcepub const fn sql_type_override_opt(self, type_str: Option<&'static str>) -> Self
pub const fn sql_type_override_opt(self, type_str: Option<&'static str>) -> Self
Set SQL type override from optional.
Sourcepub const fn precision(self, value: u8) -> Self
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)Sourcepub const fn precision_opt(self, value: Option<u8>) -> Self
pub const fn precision_opt(self, value: Option<u8>) -> Self
Set precision from optional.
Sourcepub const fn scale(self, value: u8) -> Self
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.
Sourcepub const fn decimal_precision(self, precision: u8, scale: u8) -> Self
pub const fn decimal_precision(self, precision: u8, scale: u8) -> Self
Sourcepub fn effective_sql_type(&self) -> String
pub fn effective_sql_type(&self) -> String
Get the effective SQL type name for DDL generation.
Priority:
sql_type_overrideif set- For DECIMAL/NUMERIC: uses
precisionandscalefields if set - Falls back to
sql_type.sql_name()
Sourcepub const fn primary_key(self, value: bool) -> Self
pub const fn primary_key(self, value: bool) -> Self
Set primary key flag.
Sourcepub const fn auto_increment(self, value: bool) -> Self
pub const fn auto_increment(self, value: bool) -> Self
Set auto-increment flag.
Sourcepub const fn default_opt(self, expr: Option<&'static str>) -> Self
pub const fn default_opt(self, expr: Option<&'static str>) -> Self
Set default value from optional.
Sourcepub const fn foreign_key(self, reference: &'static str) -> Self
pub const fn foreign_key(self, reference: &'static str) -> Self
Set foreign key reference.
Sourcepub const fn foreign_key_opt(self, reference: Option<&'static str>) -> Self
pub const fn foreign_key_opt(self, reference: Option<&'static str>) -> Self
Set foreign key reference from optional.
Sourcepub const fn on_delete(self, action: ReferentialAction) -> Self
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.
Sourcepub const fn on_delete_opt(self, action: Option<ReferentialAction>) -> Self
pub const fn on_delete_opt(self, action: Option<ReferentialAction>) -> Self
Set ON DELETE action from optional.
Sourcepub const fn on_update(self, action: ReferentialAction) -> Self
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.
Sourcepub const fn on_update_opt(self, action: Option<ReferentialAction>) -> Self
pub const fn on_update_opt(self, action: Option<ReferentialAction>) -> Self
Set ON UPDATE action from optional.
Sourcepub const fn alias(self, name: &'static str) -> Self
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.
Sourcepub const fn validation_alias(self, name: &'static str) -> Self
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.
Sourcepub const fn validation_alias_opt(self, name: Option<&'static str>) -> Self
pub const fn validation_alias_opt(self, name: Option<&'static str>) -> Self
Set validation alias from optional.
Sourcepub const fn serialization_alias(self, name: &'static str) -> Self
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.
Sourcepub const fn serialization_alias_opt(self, name: Option<&'static str>) -> Self
pub const fn serialization_alias_opt(self, name: Option<&'static str>) -> Self
Set serialization alias from optional.
Sourcepub const fn computed(self, value: bool) -> Self
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.
Sourcepub const fn exclude(self, value: bool) -> Self
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
}Sourcepub const fn title(self, value: &'static str) -> Self
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.
Sourcepub const fn title_opt(self, value: Option<&'static str>) -> Self
pub const fn title_opt(self, value: Option<&'static str>) -> Self
Set the schema title from optional.
Sourcepub const fn description(self, value: &'static str) -> Self
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.
Sourcepub const fn description_opt(self, value: Option<&'static str>) -> Self
pub const fn description_opt(self, value: Option<&'static str>) -> Self
Set the schema description from optional.
Sourcepub const fn schema_extra(self, value: &'static str) -> Self
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}
Sourcepub const fn schema_extra_opt(self, value: Option<&'static str>) -> Self
pub const fn schema_extra_opt(self, value: Option<&'static str>) -> Self
Set schema_extra from optional.
Sourcepub const fn default_json(self, value: &'static str) -> Self
pub const fn default_json(self, value: &'static str) -> Self
Sourcepub const fn default_json_opt(self, value: Option<&'static str>) -> Self
pub const fn default_json_opt(self, value: Option<&'static str>) -> Self
Set default_json from optional.
Sourcepub const fn has_default(self, value: bool) -> Self
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.
Sourcepub const fn const_field(self, value: bool) -> Self
pub const fn const_field(self, value: bool) -> Self
Sourcepub const fn column_constraints(
self,
constraints: &'static [&'static str],
) -> Self
pub const fn column_constraints( self, constraints: &'static [&'static str], ) -> Self
Sourcepub const fn column_comment(self, comment: &'static str) -> Self
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.
Sourcepub const fn column_comment_opt(self, comment: Option<&'static str>) -> Self
pub const fn column_comment_opt(self, comment: Option<&'static str>) -> Self
Set column comment from optional.
Sourcepub const fn column_info(self, info: &'static str) -> Self
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.
Sourcepub const fn column_info_opt(self, info: Option<&'static str>) -> Self
pub const fn column_info_opt(self, info: Option<&'static str>) -> Self
Set column info from optional.
Sourcepub const fn hybrid_sql(self, sql: &'static str) -> Self
pub const fn hybrid_sql(self, sql: &'static str) -> Self
Set hybrid SQL expression.
Sourcepub const fn hybrid_sql_opt(self, sql: Option<&'static str>) -> Self
pub const fn hybrid_sql_opt(self, sql: Option<&'static str>) -> Self
Set hybrid SQL expression from optional.
Sourcepub const fn discriminator(self, field: &'static str) -> Self
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.
Sourcepub const fn discriminator_opt(self, field: Option<&'static str>) -> Self
pub const fn discriminator_opt(self, field: Option<&'static str>) -> Self
Set discriminator from optional.
Sourcepub const fn output_name(&self) -> &'static str
pub const fn output_name(&self) -> &'static str
Get the name to use when serializing (output).
Priority: serialization_alias > alias > name
Sourcepub fn matches_input_name(&self, input: &str) -> bool
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