ColumnInfo

Struct ColumnInfo 

Source
pub struct ColumnInfo {
    pub name: &'static str,
    pub sql_type: &'static str,
    pub is_primary_key: bool,
    pub is_nullable: bool,
    pub create_time: bool,
    pub update_time: bool,
    pub unique: bool,
    pub index: bool,
    pub foreign_table: Option<&'static str>,
    pub foreign_key: Option<&'static str>,
}
Expand description

Metadata information about a database column.

This structure contains all the information needed to generate SQL table definitions and handle type conversions between Rust and SQL. It is populated automatically by the #[derive(Model)] macro based on struct field types and #[orm(...)] attributes.

§Fields

  • name - Column name (field name from struct)
  • sql_type - SQL type string (e.g., “INTEGER”, “TEXT”, “UUID”, “TIMESTAMPTZ”)
  • is_primary_key - Whether this is the primary key column
  • is_nullable - Whether NULL values are allowed (from Option)
  • create_time - Auto-populate with CURRENT_TIMESTAMP on insert
  • update_time - Auto-update timestamp on modification (future feature)
  • unique - Whether UNIQUE constraint should be added
  • index - Whether to create an index on this column
  • foreign_table - Name of referenced table (for foreign keys)
  • foreign_key - Name of referenced column (for foreign keys)

§Example

// For this field:
#[orm(size = 50, unique, index)]
username: String,

// The generated ColumnInfo would be:
ColumnInfo {
    name: "username",
    sql_type: "VARCHAR(50)",
    is_primary_key: false,
    is_nullable: false,
    create_time: false,
    update_time: false,
    unique: true,
    index: true,
    foreign_table: None,
    foreign_key: None,
}

§SQL Type Mapping

The sql_type field contains the SQL type based on the Rust type:

  • i32"INTEGER"
  • i64"BIGINT"
  • String"TEXT" or "VARCHAR(N)" with size attribute
  • bool"BOOLEAN"
  • f64"DOUBLE PRECISION"
  • Uuid"UUID"
  • DateTime<Utc>"TIMESTAMPTZ"
  • NaiveDateTime"TIMESTAMP"
  • NaiveDate"DATE"
  • NaiveTime"TIME"
  • Option<T> → Same as T, but is_nullable = true

Fields§

§name: &'static str

The column name in the database.

This is derived from the struct field name and is typically converted to snake_case when generating SQL. The r# prefix is stripped if present (for Rust keywords used as field names).

§Example

// Field: user_id: i32
name: "user_id"

// Field: r#type: String (type is a Rust keyword)
name: "r#type" // The r# will be stripped in SQL generation
§sql_type: &'static str

The SQL type of the column (e.g., “TEXT”, “INTEGER”, “TIMESTAMPTZ”).

This string is used directly in CREATE TABLE statements. It must be a valid SQL type for the target database.

§Example

// i32 field
sql_type: "INTEGER"

// UUID field
sql_type: "UUID"

// String with size = 100
sql_type: "VARCHAR(100)"
§is_primary_key: bool

Whether this column is a Primary Key.

Set to true via #[orm(primary_key)] attribute. A table should have exactly one primary key column.

§SQL Impact

  • Adds PRIMARY KEY constraint
  • Implicitly makes column NOT NULL
  • Creates a unique index automatically

§Example

#[orm(primary_key)]
id: Uuid,
// is_primary_key: true
§is_nullable: bool

Whether this column allows NULL values.

Automatically set to true when the field type is Option<T>, otherwise false for non-optional types.

§SQL Impact

  • false: Adds NOT NULL constraint
  • true: Allows NULL values

§Example

// Required field
username: String,
// is_nullable: false → NOT NULL

// Optional field
middle_name: Option<String>,
// is_nullable: true → allows NULL
§create_time: bool

Whether this column should be automatically populated with the creation timestamp.

Set via #[orm(create_time)] attribute. When true, the column gets a DEFAULT CURRENT_TIMESTAMP constraint.

§SQL Impact

  • Adds DEFAULT CURRENT_TIMESTAMP
  • Column is auto-populated on INSERT

§Example

#[orm(create_time)]
created_at: DateTime<Utc>,
// create_time: true
// SQL: created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
§update_time: bool

Whether this column should be automatically updated on modification.

Set via #[orm(update_time)] attribute. This is a future feature not yet fully implemented.

§Future Implementation

When implemented, this will:

  • Add database trigger or application-level update
  • Auto-update timestamp on every UPDATE

§Example

#[orm(update_time)]
updated_at: DateTime<Utc>,
// update_time: true (future feature)
§unique: bool

Whether this column has a UNIQUE constraint.

Set via #[orm(unique)] attribute. Ensures no two rows can have the same value in this column (NULL values may be exempt depending on database).

§SQL Impact

  • Adds UNIQUE constraint
  • Creates a unique index automatically

§Example

#[orm(unique)]
username: String,
// unique: true
// SQL: username VARCHAR(255) UNIQUE
§index: bool

Whether an index should be created for this column.

Set via #[orm(index)] attribute. Creates a database index to speed up queries that filter or sort by this column.

§SQL Impact

  • Creates separate CREATE INDEX statement
  • Index name: idx_{table}_{column}

§Example

#[orm(index)]
email: String,
// index: true
// SQL: CREATE INDEX idx_user_email ON user (email)
§foreign_table: Option<&'static str>

The name of the foreign table, if this is a Foreign Key.

Set via #[orm(foreign_key = "Table::Column")] attribute. Contains the name of the referenced table.

§Example

#[orm(foreign_key = "User::id")]
user_id: Uuid,
// foreign_table: Some("User")
§foreign_key: Option<&'static str>

The name of the foreign column, if this is a Foreign Key.

Set via #[orm(foreign_key = "Table::Column")] attribute. Contains the name of the referenced column in the foreign table.

§Example

#[orm(foreign_key = "User::id")]
user_id: Uuid,
// foreign_key: Some("id")
// SQL: FOREIGN KEY (user_id) REFERENCES user (id)

Trait Implementations§

Source§

impl Clone for ColumnInfo

Source§

fn clone(&self) -> ColumnInfo

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 ColumnInfo

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: 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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