Model¶
Model is the base class every Ferro model inherits from. The lifecycle is: define a subclass with annotated fields (which registers its table schema), connect() to a database, then perform CRUD through classmethods (create, get, where, ...) and instance methods (save, delete, refresh). Because Model is a Pydantic model, instances validate on construction and serialize like any other Pydantic object.
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:
Source code in src/ferro/models.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
Attributes¶
__ferro_composite_uniques__ = ()
class-attribute
¶
__ferro_composite_indexes__ = ()
class-attribute
¶
model_config = ConfigDict(from_attributes=True, use_attribute_docstrings=True, arbitrary_types_allowed=True)
class-attribute
instance-attribute
¶
Functions¶
__init__(**data)
¶
Initialize a model instance and normalize relationship inputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**data
|
Any
|
Field values used to construct the model. |
{}
|
Examples:
Source code in src/ferro/models.py
save(*, using=None, session=None, on_conflict=None)
async
¶
Persist the current model instance.
A transient instance (constructed with Model(...) and never saved)
is INSERTed — a duplicate primary key or unique value raises
:class:~ferro.exceptions.UniqueViolationError. A persistent instance
(fetched from the database, or previously saved) is UPDATEd by primary
key. Pass on_conflict="update" for insert-or-update semantics
regardless of persistence state (the primitive behind
:meth:upsert).
Note that model_copy() copies persistence state: saving a copy of
a persisted instance updates the same row. The UPDATE targets the
instance's current primary-key value, so mutating the PK of a
persisted instance before save() matches no row and raises. A row
inserted inside a rolled-back transaction leaves the instance marked
persisted; a later save() raises ModelDoesNotExist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
using
|
str | None
|
Connection name override. |
None
|
session
|
Session | None
|
Session scope for the operation. |
None
|
on_conflict
|
Literal['update'] | None
|
|
None
|
Raises:
| Type | Description |
|---|---|
UniqueViolationError
|
A duplicate primary key or unique value on INSERT. |
ModelDoesNotExist
|
The row behind a persisted instance no longer exists (deleted underneath, or the PK was mutated). |
ValueError
|
|
Examples:
Source code in src/ferro/models.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
delete(*, using=None, session=None)
async
¶
Delete the current model instance from storage
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
Source code in src/ferro/models.py
all(*, using=None, session=None)
async
classmethod
¶
Fetch all records for this model class
Returns:
| Type | Description |
|---|---|
list[Self]
|
A list of hydrated model instances. |
Examples:
Source code in src/ferro/models.py
get(pk, *, session=None)
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:
Source code in src/ferro/models.py
get_or_none(pk, *, session=None)
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. |
Source code in src/ferro/models.py
refresh(*, using=None, session=None)
async
¶
Reload this instance from storage using its primary key
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no primary key is available or the record no longer exists. |
Examples:
Source code in src/ferro/models.py
where(predicate, *, session=None)
classmethod
¶
Start a fluent query with an initial condition.
predicate is a lambda of shape
Callable[[QueryProxy[Self]], QueryNode], e.g.
User.where(lambda user: user.age >= 18). The lambda receives a
:class:QueryProxy whose attributes build comparisons as
:class:QueryNode instances, so predicates type-check cleanly.
Name the parameter after the model in lowercase singular (user for
User, post for Post). Column names are validated at build
time against the model's declared fields (plus shadow {fk}_id
columns).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Predicate[Self]
|
A callable that takes a :class: |
required |
Returns:
| Type | Description |
|---|---|
Query[Self]
|
A query object scoped to this model class. |
Examples:
>>> q1 = User.where(lambda user: user.archived == False) # noqa: E712
>>> q2 = User.where(lambda user: user.id == 1)
>>> isinstance(q1, Query) and isinstance(q2, Query)
True
Source code in src/ferro/models.py
select(*, session=None)
classmethod
¶
Start an empty fluent query for this model class
Returns:
| Type | Description |
|---|---|
Query[Self]
|
A query object scoped to this model class. |
Examples:
Source code in src/ferro/models.py
using(name)
classmethod
¶
create(*, session=None, **fields)
async
classmethod
¶
Create and persist a new model instance
create() is a plain INSERT: it never updates an existing row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Field values to construct the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
The newly created and persisted model instance. |
Raises:
| Type | Description |
|---|---|
UniqueViolationError
|
A row with the same primary key or unique
value already exists — use :meth: |
Examples:
Source code in src/ferro/models.py
upsert(*, session=None, **fields)
async
classmethod
¶
Insert the row, or update the existing row on primary-key conflict.
Equivalent to cls(**fields).save(on_conflict="update"). With an
autoincrement primary key left unset there is no conflict target, so
this degrades to a plain INSERT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Field values to construct the model. |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
The persisted model instance. |
Examples:
Source code in src/ferro/models.py
bulk_create(instances, *, using=None, session=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:
Source code in src/ferro/models.py
get_or_create(defaults=None, *, session=None, **fields)
async
classmethod
¶
Fetch a record by filters or create one when missing
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
defaults
|
dict[str, Any] | None
|
Values applied only when creating a new record. |
None
|
**fields
|
Exact-match filters used for lookup. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[Self, bool]
|
A tuple of |
Examples:
>>> user, created = await User.get_or_create(email="[email protected]")
>>> isinstance(created, bool)
True
Source code in src/ferro/models.py
|
update_or_create(defaults=None, *, session=None, **fields)
async
classmethod
¶
Update a matched record or create one when missing
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
defaults
|
dict[str, Any] | None
|
Values applied on update or create paths. |
None
|
**fields
|
Exact-match filters used for lookup. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[Self, bool]
|
A tuple of |