Fields¶
Field type markers¶
These are Python 3.12 generic classes that serve as type annotations for entity fields.
Sphinx autodoc cannot fully process Python 3.12 class Cls[T]: syntax, so they are
described here manually. See Defining Entities for usage examples.
- class nextorm.fields.PK[T]¶
Primary-key field — auto-generated integer by default.
On the class: returns a
ColumnExprfor use in query predicates. On an instance: returns theTvalue.Example:
class User(Entity): id: PK[int] # auto-increment integer PK id: PK[uuid7] # time-ordered UUID PK
- class nextorm.fields.Req[T]¶
Required (non-nullable) field. Maps to a
NOT NULLcolumn.Example:
class Product(Entity): name: Req[str]
- class nextorm.fields.Opt[T]¶
Optional (nullable) field — value may be
None. Maps to a nullable column.Example:
class Product(Entity): description: Opt[str]
- class nextorm.fields.Local[T]¶
Local (transient) field — never written to or read from the database. Use it to attach computed or cached state to an entity instance.
Example:
class User(Entity): _full_name: Local[str] _cache: Local[dict] = Local[dict](default=dict)
Options (passed as
Local[T](…)):default: Value or callable returned onEntity()constructionpy_check: Callable that validates the value on assignment
- class nextorm.fields.Set[T]¶
Collection relation attribute — used for both one-to-many and many-to-many.
Declare
Set[Child]on the one side andSingle[Parent]on the many side for one-to-many; declareSet[Other]on both entities for many-to-many.Options (passed as
Set[T](…)):table: (M2M only) Override join table name; defaults to{a}_{b}(alphabetical)reverse_column: (M2M only) Override the FK column name to this entity; defaults to{entity_name}_idreverse_columns: (M2M only) Override the composite FK column names; overridesreverse_column
- class nextorm.fields.Single[T]¶
Single-entity relation attribute (FK).
Use
Single[Other]for a required FK (NOT NULL, CASCADE). UseSingle[Other | None]for an optional FK (NULLABLE, SET NULL). When both sides useSingle, a UNIQUE constraint is added (one-to-one).Options (passed as
Single[T](…)):nullable: Override the nullability (defaults to auto-detect fromT | None)cascade_delete:Truefor CASCADE,Falsefor RESTRICT,None(default) for auto-derive from nullableowner: (one-to-one only)Trueto mark as owning side (has FK),Falseas non-owning,None(default) for auto-detectfk_name: Custom FK constraint name (defaults to auto-generatedfk_…)column: Override the FK column name; defaults to{relation_name}_idcolumns: Override the composite FK column names; overridescolumn
Special column types¶
- class nextorm.fields.LongStr[source]¶
Bases:
strSentinel type for large text columns.
Maps to
LONGTEXT(MariaDB),TEXT(PostgreSQL / SQLite). Use when the content may exceed the ~65 KB limit of a standard MariaDBTEXTcolumn:class Article(Entity): body: Req[LongStr]
By default
LongStrfields are lazy — they are omitted from the mainSELECTand fetched on first access via a separate query. To load eagerly, passlazy=Falseexplicitly:class Article(Entity): body: Req[LongStr] = Req(lazy=False)
- class nextorm.fields.Json[source]¶
Bases:
objectSentinel type for JSON columns.
Maps to
JSONB(PostgreSQL),JSON(MariaDB),TEXT(SQLite). Values are stored as Pythondict/listobjects and serialised/deserialised automatically:class Config(Entity): data: Req[Json]
- class nextorm.fields.DateTimeTz[source]¶
Bases:
objectSentinel type for timezone-aware datetime columns.
Maps to
TIMESTAMPTZ(PostgreSQL),DATETIME(MariaDB, assumes UTC session),TEXTISO 8601 (SQLite).PostgreSQL: psycopg / asyncpg automatically return a timezone-aware
datetimeobject. For MariaDB and SQLite the application is responsible for serialising to/from UTC:class Event(Entity): start_at: Req[DateTimeTz]
- class nextorm.fields.Vec[source]¶
Bases:
objectParameterized sentinel type for fixed-dimension vector columns.
Maps to
vector(n)(PostgreSQL with pgvector extension),TEXT(MariaDB / SQLite — JSON-serialised list).Specify the dimension with positional argument
class Article(Entity): embedding: Req[Vec[384]]
Alternatively use
Req(dimensions=n)explicitly:class Article(Entity): embedding: Req[Vec] = Req(dimensions=384)
UUID / ULID types¶
- class nextorm.fields.uuid7[source]¶
Bases:
objectSentinel type for UUID v7 auto-generated primary keys.
Use in entity annotations to declare a time-ordered, sortable UUID PK:
class Event(Entity): id: PK[uuid7]
The field is stored as
uuid.UUIDin Python and mapped toUUID(PostgreSQL),CHAR(36)(MariaDB), orTEXT(SQLite). A UUID v7 value is auto-generated before every INSERT if the field is not already set.
- class nextorm.fields.uuid4[source]¶
Bases:
objectSentinel type for UUID v4 auto-generated primary keys.
Use like
PK[uuid4]. Same storage asuuid7but uses random UUID v4 generation (not time-ordered).
- class nextorm.fields.ulid[source]¶
Bases:
objectSentinel type for ULID auto-generated primary keys.
Use like
PK[ulid]. The field is stored as aULIDstring (26-character Crockford base32) mapped toCHAR(26)in DDL.
- class nextorm.fields.ULID[source]¶
Bases:
str26-character Crockford base32 ULID value type.
Used as the Python type for
PK[ulid]fields. ULID values are stored asCHAR(26)(MariaDB),TEXT(SQLite), or the native UUID column cast to base32 (not supported — stored as TEXT on all backends).A
ULIDinstance is an ordinary string and sorts lexicographically in creation order (time-order), which is the key property of ULIDs.
Metadata classes¶
FieldSpec and RelationSpec
are the internal dataclasses that hold the resolved field configuration after
EntityMeta processes the class body. They are exposed
publicly for introspection (e.g. Entity._fields_["name"].spec) but should
not be used directly as class-body values — use the marker-call syntax
(Req(...), Opt(...), Single(...), etc.) instead.
- class nextorm.fields.FieldSpec[source]¶
Bases:
objectMetadata that describes a single persistent column.
EntityMetacreates aFieldSpecautomatically for every annotated field. You can set options through the marker call syntax (validated at class-definition time):class Product(Entity): name: Req[str] = Req(max_len=120, unique=True) price: Req[float] = Req(min=0.0) updated_at: Req[datetime] = Req(volatile=True, sql_default="CURRENT_TIMESTAMP")
Parameters¶
- auto:
Truefor auto-increment integer PKs or UUID/ULID PKs generated by Python before INSERT. Set automatically byPK; rarely needed manually.- autostrip:
Strip leading/trailing whitespace on every string assignment (default
FalseforLongStr;Truefor plainstrfields when set viaStrTypeOpts).- column:
Override the database column name. Defaults to the attribute name.
- db_encoding:
Database character encoding for the column, e.g.
"utf8mb4"for full Unicode support on MariaDB. Ignored on PostgreSQL and SQLite.- default:
Python-side default value or zero-argument callable. Applied before
INSERTwhen the field was not explicitly set. Use_MISSING(the module-level sentinel) to indicate no default.- dimensions:
Number of dimensions for
Veccolumns (e.g.384,1536). Set automatically when you writeReq[Vec[384]].- index:
Create a single-column index on this column.
- lazy:
Exclude the column from the main
SELECT; load its value on first access via a separate query. Set automatically forLongStrfields.- max:
Inclusive upper bound enforced in Python on every assignment.
- max_len:
Maximum string length enforced in Python and reflected in DDL (
VARCHAR(n)).- min:
Inclusive lower bound enforced in Python on every assignment.
- nullable:
Allow SQL
NULL. Set automatically forOptfields.- precision:
Total significant digits for
DECIMAL/NUMERICcolumns, or fractional-second precision forDATETIME/TIMESTAMPcolumns.- primary_key:
Mark this column as (part of) the primary key.
- py_check:
Zero-argument callable
(value) -> bool; raisesValueErrorwhen it returns falsy. Runs on every assignment.- scale:
Digits after the decimal point for
DECIMAL/NUMERICcolumns.- size:
Integer column bit-width — one of
8,16,32, or64.- sql_default:
Raw SQL expression used in DDL
DEFAULT, e.g."CURRENT_TIMESTAMP". Not applied in Python — usedefaultfor that.- sql_type:
Override the inferred SQL type string, e.g.
"JSONB"or"GEOMETRY".- unique:
Add a
UNIQUEconstraint on this column.- unsigned:
Add the
UNSIGNEDmodifier (MariaDB only; ignored on other backends).- uuid_auto:
Python-side UUID/ULID generation strategy:
"v7","v4", or"ulid". Set automatically by thePKmarker for UUID/ULID types.- volatile:
Exclude this column from
UPDATEstatements. Use for columns whose value is maintained entirely by a database trigger orDEFAULTexpression.
- default: Any¶
- max: Any = None¶
- min: Any = None¶
- __init__(auto=False, autostrip=False, column=None, db_encoding=None, default=<factory>, dimensions=None, index=False, lazy=False, max=None, max_len=None, min=None, nullable=False, precision=None, primary_key=False, py_check=None, scale=None, sql_default=None, sql_type=None, size=None, unique=False, uuid_auto=None, unsigned=False, volatile=False)¶
- Parameters:
auto (bool)
autostrip (bool)
column (str | None)
db_encoding (str | None)
default (Any)
dimensions (int | None)
index (bool)
lazy (bool)
max (Any)
max_len (int | None)
min (Any)
nullable (bool)
precision (int | None)
primary_key (bool)
scale (int | None)
sql_default (str | None)
sql_type (str | None)
size (int | None)
unique (bool)
uuid_auto (str | None)
unsigned (bool)
volatile (bool)
- Return type:
None
- class nextorm.fields.RelationSpec[source]¶
Bases:
objectMetadata that describes a relation (FK or join-table) column.
EntityMetacreates aRelationSpecautomatically fromSingleandSetannotations.class Comment(Entity): post: Single[Post](fk_name="fk_comment_post", cascade_delete=True)
Note
kindandtargetare always filled in byEntityMetafrom the annotation.Parameters —
Singlerelations¶- nullable:
True→ NULLABLE FK column with ON DELETE SET NULL.False(default) → NOT NULL column with ON DELETE CASCADE.- cascade_delete:
True— force ON DELETE CASCADE regardless ofnullable.False— force ON DELETE RESTRICT regardless ofnullable.None(default) — derive automatically fromnullable.- column:
Override the FK column name. Defaults to
{attr_name}_id.- columns:
Composite FK column names (list). Mutually exclusive with
column.- fk_name:
Override the foreign-key constraint name in DDL.
- owner:
One-to-one only.
True= this side owns the FK column (UNIQUE constraint added here).False= non-owning back-reference.None(default) = auto-detect fromnullableor alphabetical order.- primary_key:
Truewhen the FK column is also the table’s primary key. Set automatically byPKwhen subscripted with an entity type.
Parameters —
Setrelations¶- table:
Override the many-to-many join table name.
- reverse_column:
Override the join-table column that points back to the declaring entity.
- reverse_columns:
Composite version of
reverse_column.
Parameters — both kinds¶
- reverse:
Attribute name of the reverse relation on the target entity, used for explicit back-reference wiring when NextORM cannot infer it automatically.
- __init__(kind='', target=None, cascade_delete=None, column=None, columns=None, fk_name=None, nullable=False, owner=None, primary_key=False, reverse=None, reverse_column=None, reverse_columns=None, table=None)¶
- Parameters:
- Return type:
None
- class nextorm.fields.LocalSpec[source]¶
Bases:
objectMetadata for a single local (transient) field.
Created by
EntityMetafrom aLocalannotation and stored incls._locals_. Never reaches the database layer — it exists solely to support Python-side behaviour (defaults and validation) that mirrors whatFieldSpecprovides for persistent fields.Parameters¶
- default:
Value or zero-argument callable applied before the first assignment when the field was not supplied to
__init__. If omitted, accessing an uninitialisedLocalfield raisesAttributeErroruntil the field is assigned — typically insideafter_load().- py_check:
Callable
(value) -> boolrun on every assignment. RaisesValueErrorwhen it returnsFalse.
- default: Any¶
Composite constraints¶
- nextorm.fields.composite_key(*field_names)[source]¶
Declare a multi-column unique constraint (equivalent to
UNIQUE (a, b)).Place this inside the entity body as a class attribute:
class Booking(Entity): slot: Req[int] room: Req[int] _ck_slot_room_ = composite_key("slot", "room")
- Parameters:
field_names (str)
- Return type:
- nextorm.fields.composite_index(*field_names)[source]¶
Declare a multi-column non-unique index (equivalent to
INDEX (a, b)).Place this inside the entity body as a class attribute:
class LogEntry(Entity): source: Req[str] level: Req[str] _idx_source_level_ = composite_index("source", "level")
- Parameters:
field_names (str)
- Return type:
- nextorm.fields.PrimaryKey(*field_names)[source]¶
Declare a composite primary key spanning two or more fields.
field_names may be scalar field names or relation names (
Singlerelations whose FK column then becomes part of the PK). All referenced relations must be required (non-nullable).Place this inside the entity body as a class attribute:
class OrderLine(Entity): order: Single[Order] product: Single[Product] quantity: Req[int] _pk_ = PrimaryKey("order", "product") class Enrollment(Entity): student_id: Req[int] course_id: Req[int] grade: Opt[str] _pk_ = PrimaryKey("student_id", "course_id")
- Parameters:
field_names (str)
- Return type:
- class nextorm.fields.CompositeConstraint[source]¶
Bases:
objectMulti-column index, unique constraint, or primary key declared at class level.
Do not instantiate directly — use
composite_key(),composite_index(), orPrimaryKey()instead.