cquill/schema/field
Types
Constraints that can be applied to fields
pub type Constraint {
NotNull
Unique
PrimaryKey
ForeignKey(
table: String,
column: String,
on_delete: ForeignKeyAction,
)
Check(name: String, expression: String)
MinValue(min: Int)
MaxValue(max: Int)
MinLength(min: Int)
MaxLength(max: Int)
Pattern(regex: String)
}
Constructors
-
NotNullField cannot be NULL
-
UniqueField must have unique values (per-column unique)
-
PrimaryKeyField is the primary key (or part of composite key)
-
ForeignKey( table: String, column: String, on_delete: ForeignKeyAction, )Field references another table
-
Check(name: String, expression: String)Custom check constraint
-
MinValue(min: Int)Minimum value constraint (for numeric types)
-
MaxValue(max: Int)Maximum value constraint (for numeric types)
-
MinLength(min: Int)Minimum length constraint (for string types)
-
MaxLength(max: Int)Maximum length constraint (for string types)
-
Pattern(regex: String)Regex pattern constraint (for string types)
Default value for a field
pub type Default {
DefaultValue(dynamic.Dynamic)
DefaultFunction(function_name: String)
DefaultAutoIncrement
}
Constructors
-
DefaultValue(dynamic.Dynamic)Static default value
-
DefaultFunction(function_name: String)Database function as default (e.g., “now()”, “gen_random_uuid()”)
-
DefaultAutoIncrementUse database’s auto-increment/serial
Complete field definition with all metadata
pub type Field {
Field(
name: String,
field_type: FieldType,
default: option.Option(Default),
constraints: List(Constraint),
comment: option.Option(String),
)
}
Constructors
-
Field( name: String, field_type: FieldType, default: option.Option(Default), constraints: List(Constraint), comment: option.Option(String), )Arguments
- name
-
Field name (column name in database)
- field_type
-
Field type
- default
-
Default value if any
- constraints
-
Constraints applied to this field
- comment
-
Documentation/comment for this field
Represents the type of a database field. These map to common SQL types across different databases.
pub type FieldType {
Integer
BigInteger
Float
Decimal(precision: Int, scale: Int)
String
Char(length: Int)
Boolean
DateTime
Date
Time
Binary
Uuid
Json
Array(FieldType)
Nullable(FieldType)
Enum(name: String, values: List(String))
Custom(type_name: String)
}
Constructors
-
IntegerInteger type (32-bit signed) SQL: INTEGER, INT, INT4
-
BigIntegerBig integer type (64-bit signed) SQL: BIGINT, INT8, BIGSERIAL
-
FloatFloating point number SQL: FLOAT, DOUBLE PRECISION, REAL
-
Decimal(precision: Int, scale: Int)Decimal/numeric with precision SQL: DECIMAL, NUMERIC
-
StringVariable-length string SQL: VARCHAR, TEXT, CHARACTER VARYING
-
Char(length: Int)Fixed-length string SQL: CHAR, CHARACTER
-
BooleanBoolean value SQL: BOOLEAN, BOOL
-
DateTimeTimestamp with timezone SQL: TIMESTAMP WITH TIME ZONE, TIMESTAMPTZ
-
DateDate only (no time) SQL: DATE
-
TimeTime only (no date) SQL: TIME, TIME WITHOUT TIME ZONE
-
BinaryBinary data SQL: BYTEA, BLOB, BINARY
-
UuidUUID/GUID SQL: UUID
-
JsonJSON data SQL: JSON, JSONB
-
Array(FieldType)Array of another type SQL: type[], ARRAY
-
Nullable(FieldType)Nullable wrapper - field can be NULL This is separate from constraints to allow type composition
-
Enum(name: String, values: List(String))Enum type with allowed values SQL: ENUM (Postgres), CHECK constraint (others)
-
Custom(type_name: String)Custom/adapter-specific type For types not covered by the standard set
Actions for foreign key ON DELETE/ON UPDATE
pub type ForeignKeyAction {
NoAction
Restrict
Cascade
SetNull
SetDefault
}
Constructors
-
NoAction -
Restrict -
Cascade -
SetNull -
SetDefault
Values
pub fn decimal(name: String, precision: Int, scale: Int) -> Field
Create a decimal field with precision and scale
pub fn default_function(
field: Field,
function_name: String,
) -> Field
Add a database function as default
pub fn default_value(
field: Field,
value: dynamic.Dynamic,
) -> Field
Add a static default value
pub fn enum_(
name: String,
enum_name: String,
values: List(String),
) -> Field
Create an enum field
pub fn get_base_type(field: Field) -> FieldType
Get the field’s base type (unwrapping Nullable if present)
pub fn get_constraints(
field: Field,
predicate: fn(Constraint) -> Bool,
) -> List(Constraint)
Get all constraints of a specific type
pub fn has_constraint(
field: Field,
predicate: fn(Constraint) -> Bool,
) -> Bool
Check if field has a specific constraint
pub fn is_auto_increment(field: Field) -> Bool
Check if field has auto-increment
pub fn is_foreign_key_constraint(constraint: Constraint) -> Bool
Check if constraint is ForeignKey
pub fn is_not_null_constraint(constraint: Constraint) -> Bool
Check if constraint is NotNull
pub fn is_numeric_type(field_type: FieldType) -> Bool
Check if a field type is numeric
pub fn is_primary_key_constraint(constraint: Constraint) -> Bool
Check if constraint is PrimaryKey
pub fn is_temporal_type(field_type: FieldType) -> Bool
Check if a field type is temporal (date/time related)
pub fn is_text_type(field_type: FieldType) -> Bool
Check if a field type is text-like
pub fn is_unique_constraint(constraint: Constraint) -> Bool
Check if constraint is Unique
pub fn references(
field: Field,
table: String,
column: String,
) -> Field
Add foreign key constraint
pub fn references_with_action(
field: Field,
table: String,
column: String,
on_delete: ForeignKeyAction,
) -> Field
Add foreign key with ON DELETE action
pub fn type_name(field_type: FieldType) -> String
Get a human-readable name for a field type