Table and type definitions
Model, Schema, and the compiler share a backend independent table representation: TableDefinition. Integer, String, and the other types are ColumnDefinition objects; structured constraints complete the physical schema. This reference covers public metadata. The type catalog contains all 39 logical types, their 40 factories, constructors, arguments, and examples; modifiers explain every fluent operation.
from orionis.database.schema.column import Columnfrom orionis.orm import Modelfrom orionis.orm.schema import TableDefinitionfrom orionis.orm.schema.constraints import TableIndex, UniqueConstraint
class ProjectsV1Schema: table_definition = TableDefinition( name="projects", columns={ "id": Column.id(), "name": Column.string("name", 120), "slug": Column.string("slug", 160), }, primary_key="id", unique_constraints=(UniqueConstraint(("slug",), name="uq_project_slug"),), indexes=(TableIndex(("name",), name="ix_project_name"),), )
class Project(Model): table_definition = ProjectsV1Schema.table_definition timestamps = False fillable = ("name", "slug")The definition executes no DDL. A migration must call await Schema.createFromDefinition(ProjectsV1Schema.table_definition); Project can be used once its table exists. Preserve V1 and introduce a new definition for future changes to keep history reproducible. Model definitions explain how inline columns and explicit metadata are integrated.
TableDefinition
Section titled “TableDefinition”A dataclass with frozen=True, slots=True, eq=False. Fields accept positional or keyword arguments in this order. Frozen prevents field reassignment but does not deeply freeze the columns dictionary or its ColumnDefinition values: do not mutate a version used by historical migrations.
name: strLogical name without the connection prefix. Required.
columns
Section titled “columns”columns: dict[str, ColumnDefinition] = field(default_factory=dict)A fresh, initially empty name → ColumnDefinition dictionary. Insertion order determines column order. The dictionary key is the SQL name used by the compiler.
primary_key
Section titled “primary_key”primary_key: str = 'id'Single key name, default id. This neither creates a missing column nor marks an arbitrary definition as primary by itself: declare primary() on the column, or use composite_primary_key for a composite constraint.
schema
Section titled “schema”schema: str | None = NoneOwning SQL schema; None uses the default. This does not create the schema automatically.
comment
Section titled “comment”comment: str | None = NoneTable comment or None; storage is backend dependent.
composite_primary_key
Section titled “composite_primary_key”composite_primary_key: tuple[str, ...] | None = NoneOrdered tuple or None; translated to PrimaryKeyConstraint. Takes precedence over the simple key representation for the schema. This does not turn Model.primary_key into a supported tuple.
unique_constraints
Section titled “unique_constraints”unique_constraints: tuple[UniqueConstraint, ...] = ()Tuple of UniqueConstraint values, default (). Supports simple or composite constraints with optional names.
foreign_keys
Section titled “foreign_keys”foreign_keys: tuple[CompositeForeignKey, ...] = ()Tuple of CompositeForeignKey values, default (). Aligns local and referenced columns by position.
indexes
Section titled “indexes”indexes: tuple[TableIndex, ...] = ()Tuple of TableIndex values, default (). Retains each index column order, name, and uniqueness.
columnNames
Section titled “columnNames”columnNames() -> tuple[str, ...]columnNames returns an ordered name tuple; hasColumn checks membership and returns bool. Both read metadata and do not inspect the database.
hasColumn
Section titled “hasColumn”hasColumn(name: str) -> boolcolumnNames returns an ordered name tuple; hasColumn checks membership and returns bool. Both read metadata and do not inspect the database.
definition = ProjectsV1Schema.table_definitionnames = definition.columnNames()contains_slug = definition.hasColumn("slug")Structured constraints
Section titled “Structured constraints”These are frozen/slotted dataclasses with no SQL access. Required arguments have no default; optional defaults appear in the signatures. There is no comprehensive early validation of lengths, columns, or server compatibility. Supply nonempty tuples, existing columns, and equal counts of local and referenced columns.
UniqueConstraint
Section titled “UniqueConstraint”UniqueConstraint(columns: tuple[str, ...], name: str | None = None)columns identifies jointly unique columns; name leaves naming to the backend when None.
TableIndex
Section titled “TableIndex”TableIndex(columns: tuple[str, ...], name: str | None = None, unique: bool = False)columns preserves order; name=None produces ix_<logical_table>_<columns>; unique requests uniqueness.
CompositeForeignKey
Section titled “CompositeForeignKey”CompositeForeignKey(columns: tuple[str, ...], ref_table: str, ref_columns: tuple[str, ...], name: str | None = None)ref_table accepts schema.table. columns and ref_columns align by position. No ondelete, onupdate, or deferrable arguments exist.
ForeignReference
Section titled “ForeignReference”ForeignReference(table: str, column: str)Contains a single column reference. Its constructor stores values; parse validates table.column syntax.
ForeignReference.parse(reference: str) -> ForeignReferenceForeignReference.qualified() -> strfrom orionis.orm.schema.constraints import ForeignReference
reference = ForeignReference.parse("users.id")assert reference.table == "users"assert reference.column == "id"assert reference.qualified() == "users.id"parse is a classmethod requiring exactly two nonempty segments; schema.table.column, incomplete strings, or nonstrings raise ValueError. qualified is an instance method returning table.column. Use CompositeForeignKey for schema qualified targets.
Composite key example
Section titled “Composite key example”The articles table must exist with a compatible UNIQUE or PRIMARY KEY over (tenant_id, id). Enable foreign keys on SQLite to enforce references.
from orionis.database.schema.column import Columnfrom orionis.orm.schema import TableDefinitionfrom orionis.orm.schema.constraints import CompositeForeignKey
TRANSLATIONS_V1 = TableDefinition( name="article_translations", columns={ "tenant_id": Column.integer("tenant_id"), "article_id": Column.integer("article_id"), "locale": Column.string("locale", 10), "title": Column.string("title", 120), }, composite_primary_key=("tenant_id", "article_id", "locale"), foreign_keys=( CompositeForeignKey( columns=("tenant_id", "article_id"), ref_table="articles", ref_columns=("tenant_id", "id"), name="fk_translation_article", ), ),)ColumnOptions
Section titled “ColumnOptions”A frozen=True/slots=True dataclass. Its constructor accepts all fields below, positionally or by keyword, with the displayed defaults. ColumnDefinition copies each option onto its own attributes; prefer concrete constructors from the catalog.
length
Section titled “length”length: int | None = NoneText/binary length.
precision
Section titled “precision”precision: int | None = NoneNumeric precision.
scale: int | None = NoneDecimal scale.
decimal_return_scale
Section titled “decimal_return_scale”decimal_return_scale: int | None = NoneScale for Decimal conversion.
as_decimal
Section titled “as_decimal”as_decimal: bool = TrueDecimal conversion; public type constructors call this asdecimal. Float/Double change it to False.
enum_values
Section titled “enum_values”enum_values: tuple[str, ...] = ()Enum string members.
enum_name
Section titled “enum_name”enum_name: str | None = NoneEnum type name; constructors call this name.
native_enum
Section titled “native_enum”native_enum: bool = TrueRequest native ENUM.
validate_strings
Section titled “validate_strings”validate_strings: bool = FalseEnum string validation.
create_constraint
Section titled “create_constraint”create_constraint: bool = FalseCHECK for emulated booleans/enums.
constraint_name
Section titled “constraint_name”constraint_name: str | None = NoneBoolean CHECK name.
timezone
Section titled “timezone”timezone: bool = FalseDate/time timezone support where available.
collation
Section titled “collation”collation: str | None = NoneText collation.
as_uuid
Section titled “as_uuid”as_uuid: bool = TrueConvert to uuid.UUID objects.
native_uuid
Section titled “native_uuid”native_uuid: bool = TrueNative UUID representation where available.
item_type
Section titled “item_type”item_type: ColumnDefinition | None = NoneARRAY element type; retained but has no SQL builder.
dimensions
Section titled “dimensions”dimensions: int | None = NoneARRAY dimensions; retained but not materializable.
as_tuple
Section titled “as_tuple”as_tuple: bool = FalseARRAY results as tuples; retained without SQL support.
zero_indexes
Section titled “zero_indexes”zero_indexes: bool = FalseARRAY index conversion; retained without SQL support.
none_as_null
Section titled “none_as_null”none_as_null: bool = FalseJSON None as SQL NULL instead of JSON null.
native
Section titled “native”native: bool = TrueNative INTERVAL where available.
second_precision
Section titled “second_precision”second_precision: int | None = NoneINTERVAL second precision.
day_precision
Section titled “day_precision”day_precision: int | None = NoneINTERVAL day precision.
protocol
Section titled “protocol”protocol: int = 5Pickle protocol; the literal default is 5.
pickler
Section titled “pickler”pickler: object | None = NoneCustom pickler retained; SQLCompiler does not forward it.
comparator
Section titled “comparator”comparator: Callable[[object, object], bool] | None = NoneComparator retained; SQLCompiler does not use it. No concrete type constructor exposes it.
impl: object | None = NoneCustom binary implementation retained; SQLCompiler does not forward it.
from orionis.orm.schema.column import ColumnDefinition, ColumnOptionsfrom orionis.orm import ColumnType
column = ColumnDefinition( ColumnType.STRING, ColumnOptions(length=120),).nullable()column.name = "title"Additional public ColumnDefinition state
Section titled “Additional public ColumnDefinition state”In addition to the options above, available properties are name (initially empty string), column_type (required ColumnType), is_primary/is_nullable/is_unique/has_index/is_auto_increment (False), comment_text (None), foreign_ref (None or ForeignReference), and default_value (a private absence sentinel). Use hasDefault rather than comparing that sentinel. Configure these properties through fluent methods. These metadata expose neither business data validation nor direct backend access.
ColumnType
Section titled “ColumnType”A StrEnum with 39 identifiers. Values are logical metadata, not a promise of available native storage. ARRAY, MATCH_TYPE, NUMERIC_COMMON, and SCHEMA_TYPE translation is not registered; creating tables with them raises QueryException.
| Member | Value |
|---|---|
BIG_INTEGER | big_integer |
BOOLEAN | boolean |
DATE | date |
DATETIME | datetime |
DOUBLE | double |
ENUM | enum |
FLOAT | float |
INTEGER | integer |
INTERVAL | interval |
LARGE_BINARY | large_binary |
MATCH_TYPE | match_type |
NUMERIC | numeric |
NUMERIC_COMMON | numeric_common |
PICKLE_TYPE | pickle_type |
SCHEMA_TYPE | schema_type |
SMALL_INTEGER | small_integer |
STRING | string |
TEXT | text |
TIME | time |
UNICODE | unicode |
UNICODE_TEXT | unicode_text |
UUID | uuid |
ARRAY | array |
BIGINT | bigint |
BINARY | binary |
BLOB | blob |
CHAR | char |
CLOB | clob |
DECIMAL | decimal |
DOUBLE_PRECISION | double_precision |
INT | int |
JSON | json |
NCHAR | nchar |
NVARCHAR | nvarchar |
REAL | real |
SMALLINT | smallint |
TIMESTAMP | timestamp |
VARBINARY | varbinary |
VARCHAR | varchar |
Internal objects visible from modules
Section titled “Internal objects visible from modules”DefinitionBucket() is an implementation accumulator, not an alternative table creation API. Its public containers columns={}, primary_columns=[], unique_constraints=[], foreign_keys=[], indexes=[], and kwargs={} group definitions while Schema processes a blueprint. Schema._createTable has a reserved prefix and is not public facade API. Use the five documented Schema operations to create tables.