Model API¶
Complete reference for the Model base class and related methods.
Model
¶
Bases: BaseModel
Provide the base class for all Ferro models
Inheriting from this class registers schema metadata with the Rust core and exposes high-performance CRUD and query entrypoints.
Composite unique constraints: declare a typing.ClassVar named
__ferro_composite_uniques__ as a tuple of tuples of column names
(for example (("user_id", "org_id"),)) to enforce uniqueness on those
columns together. This is separate from per-column uniqueness
(Field(unique=True) on the field, Annotated[..., Field(unique=True)],
or Annotated[..., FerroField(unique=True)]), each of which applies to a
single column only. Default many-to-many join tables get a
composite unique on their two foreign-key columns automatically.
Composite indexes: declare a typing.ClassVar named
__ferro_composite_indexes__ as a tuple of tuples of column names
(for example (("user_id", "created_at"),)) for non-unique multi-column
indexes. Validation rules mirror __ferro_composite_uniques__: each
inner tuple must contain at least two columns, columns must exist on the
model, and order is preserved (matters for leftmost-prefix optimization).
For single-column indexes use Field(index=True). Default many-to-many
join tables get a non-unique reverse-direction composite index
automatically; opt out with ManyToMany(reverse_index=False).
Examples:
Functions¶
create(**fields)
async
classmethod
¶
bulk_create(instances, *, using=None)
async
classmethod
¶
Persist multiple instances in a single bulk operation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instances
|
list[Self]
|
Model instances to persist. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of records inserted. |
Examples:
get(pk)
async
classmethod
¶
Fetch one record by primary key value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pk
|
Any
|
Primary key value to fetch a single record. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
The matching model instance. |
Raises:
| Type | Description |
|---|---|
ModelDoesNotExist
|
When no row exists for this primary key. Use
:meth: |
Examples:
get_or_none(pk)
async
classmethod
¶
Fetch one record by primary key, or return None if no row exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pk
|
Any
|
Primary key value to fetch a single record. |
required |
Returns:
| Type | Description |
|---|---|
Self | None
|
The matching model instance, or None when no record exists. |
where(node)
classmethod
¶
Start a fluent query with an initial condition.
Accepts either a :class:QueryNode (built with operator syntax or
with :func:ferro.query.col) or a lambda predicate of shape
Callable[[QueryProxy[Self]], QueryNode]. See
docs/concepts/query-typing.md for the trade-offs between the
three styles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
QueryNode | Predicate[Self]
|
A |
required |
Returns:
| Type | Description |
|---|---|
Query[Self]
|
A query object scoped to this model class. |
Examples:
select()
classmethod
¶
Start an empty fluent query for this model class
Returns:
| Type | Description |
|---|---|
Query[Self]
|
A query object scoped to this model class. |
Examples:
all(*, using=None)
async
classmethod
¶
delete(*, using=None)
async
¶
save(*, using=None)
async
¶
refresh(*, using=None)
async
¶
See Also¶
- Models & Fields Guide
- Queries Guide — fetch by primary key (
get/get_or_none) - Exceptions —
ModelDoesNotExist - Mutations Guide