Models
Active-record models with mass assignment, casts, relationships, lifecycle hooks, and soft deletes.
Alata models give every database table a rich Python class. A model class is your query entry point (User.where(...), User.find(1)) and each instance is a row you can mutate, save, serialize, and delete. Underneath, models compile to SQLAlchemy 2.0 core statements, and every operation has a sync and an async form.
Defining a Model
Extend Model, set __tablename__, and declare your columns. Alata ships concise column helpers so definitions stay short:
from alata.db import Model, pk, str_col, text_col, bool_col, int_col, json_col, dt_col
class User(Model):
__tablename__ = "users"
__fillable__ = ["name", "email", "age", "bio"]
id = pk()
name = str_col()
email = str_col(unique=True)
age = int_col(nullable=True)
bio = text_col()
active = bool_col(default=True)
meta = json_col()Table names are always explicit — set __tablename__ on every model; nothing is derived from the class name.
Column helpers
alata.db exports a shorthand factory for each common column type:
| Helper | Column type | Notes |
|---|---|---|
pk() | INTEGER auto-increment primary key | |
big_pk() | BIGINT auto-increment primary key | |
uuid_pk() | VARCHAR(36) primary key | For UUID / ULID values you assign yourself |
str_col(length=255, **kw) | VARCHAR | Not nullable by default |
text_col(**kw) | TEXT | Nullable by default |
int_col(**kw) | INTEGER | |
big_int_col(**kw) | BIGINT | |
small_int_col(**kw) | SMALLINT | |
bool_col(default=False, **kw) | BOOLEAN | |
float_col(**kw) | FLOAT | |
numeric_col(precision=10, scale=2, **kw) | NUMERIC | |
json_col(**kw) | JSON (JSONB on PostgreSQL) | Nullable by default |
dt_col(**kw) | TIMESTAMP WITH TIME ZONE | Nullable by default |
date_col(**kw) | DATE | Nullable by default |
Each helper accepts standard column keyword arguments — nullable=True, unique=True, index=True, default=.... For anything the helpers don't cover, the raw SQLAlchemy types (Column, String, Integer, DateTime, ...) are re-exported from alata.db too.
Class-level configuration
Every knob a model understands is a class attribute:
class Post(Model):
__tablename__ = "posts"
__connection__ = "default" # named connection from config/database.py
__fillable__ = ["title", "body"] # mass-assignable columns
__hidden__ = ["body"] # stripped from to_dict()/to_json()
__visible__ = [] # if set, ONLY these columns serialize
__appends__ = ["excerpt"] # computed properties added to to_dict()
__casts__ = {"body": "json"} # attribute casting
__relationships__ = {} # see Relationships below
__global_scopes__ = {} # see Global Scopes belowTimestamps
Every model automatically has created_at and updated_at columns. created_at is set when a row is inserted and updated_at refreshes on every update — you never manage them by hand. The timestamps() blueprint method creates the matching columns in your migrations.
Model timestamps are stored and returned as timezone-aware UTC datetimes, including deleted_at and values declared with dt_col(). Use date helpers to display them in your application's timezone.
Primary keys
The primary key is whatever column you mark as primary (pk(), big_pk(), uuid_pk(), or Column(..., primary_key=True)). Methods like find() and save() discover the primary key from the column definition — composite keys resolve to the first primary key column.
Retrieving Models
Model classes start queries directly. Any where-style class method returns a query builder you can keep chaining:
from app.models.user import User
users = User.all() # every row, as a Collection
user = User.find(1) # by primary key, or None
users = User.find_many([1, 2, 3]) # by several primary keys
user = User.find_or_fail(1) # raises ModelNotFoundError if missing
user = User.first_where("email", "a@b.com") # shortcut for where(...).first()
adults = User.where("age", ">=", 18).order_by("name").get()ModelNotFoundError is importable from alata.db.
The query starters available as class methods are where, where_null, where_not_null, where_in, where_not_in, where_between, where_raw, has, where_has, with_, with_count, and the with_sum / with_max / with_min / with_avg aggregate family. Everything else the query builder offers — ordering, pagination, aggregates, pluck, locking — chains off those.
Async retrieval
Every read and write has an async twin prefixed with a. Use them inside request handlers and other coroutine code:
users = await User.aall()
user = await User.afind(1)
user = await User.afind_or_fail(1)
user = await User.afirst_where("email", "a@b.com")
users = await User.where("active", True).aget()Chunking large tables
Avoid loading a huge table into memory at once:
def handle_batch(batch: list) -> None:
for user in batch:
...
User.chunk(500, handle_batch) # batches of 500; return False to stop
User.each(lambda u: ..., size=100) # one record at a time, fetched in chunks
for user in User.lazy(chunk_size=100): # generator over the whole table
...The async counterparts are achunk, aeach, and alazy.
Building instances from raw rows: hydrate
Model.hydrate() turns raw row mappings — plain dicts, or rows from DB.select() — into model instances, with read casts (__casts__) applied. It runs no queries and fires no events; the instances are built directly from the data you give it:
from alata.db import DB
rows = DB.select("SELECT * FROM users WHERE score > :s", {"s": 90})
users = User.hydrate(rows) # Collection[User], casts applied
users[0].is_dirty() # False — originals are snapshotted for dirty checkingKeys that match mapped columns become column attributes. Extra keys — computed columns from hand-written SQL — are attached as plain instance attributes:
users = User.hydrate([{"id": 1, "name": "Ada", "email": "ada@example.com", "post_count": 7}])
users[0].post_count # 7hydrate() never filters the rows it is given — global scopes, including soft-delete filtering, are not applied. You composed the query; the instances reflect it exactly.
hydrate() is also what the query builder's from_statement() uses to turn entity-shaped rows from hand-built SQLAlchemy statements back into models.
Creating and Updating
create
create() inserts a row and returns a fully populated instance — including the database-assigned primary key:
user = User.create({"name": "Alice", "email": "alice@example.com"})
user.id # populated
user = await User.acreate({"name": "Alice", "email": "alice@example.com"})Mass assignment
create(), acreate(), insert(), and ainsert() silently drop any key not listed in __fillable__. This protects you from a request payload writing to columns it shouldn't:
class User(Model):
__fillable__ = ["name", "email"]
user = User.create({"name": "Eve", "email": "eve@example.com", "is_admin": True})
# "is_admin" was discarded — only name and email were insertedIf __fillable__ is empty (the default), no filtering happens — every key in the dict is written. Always declare __fillable__ on models that accept user input.
Constructing an instance directly and calling save() bypasses the fillable filter entirely — you are assigning attributes yourself, so you are trusted:
user = User(name="Bob", email="bob@example.com")
user.save() # INSERT — instance has no primary key yet
user.name = "Robert"
user.save() # UPDATE — instance has a primary key
await user.asave() # async variantBulk inserts and upserts
User.insert([
{"name": "A", "email": "a@example.com"},
{"name": "B", "email": "b@example.com"},
]) # single INSERT, no instances returned
User.upsert(
rows=[{"email": "a@example.com", "name": "A2"}],
unique_by=["email"],
update_cols=["name"],
) # INSERT ... ON CONFLICT (email) DO UPDATE SET nameOmitting update_cols updates only the columns the supplied rows name, minus unique_by, the primary key, and created_at — see Upserts for the full contract.
Async: ainsert, aupsert.
first_or_create and update_or_create
# Return the matching row, or create it (search attrs + create attrs merged)
tag = Tag.first_or_create({"name": "python"}, {"color": "blue"})
# Update the matching row, or create it
plan = Plan.update_or_create({"user_id": user.id}, {"tier": "pro"})Both run the create inside a transaction and gracefully recover if a concurrent request wins the race on a unique constraint. Async: afirst_or_create, aupdate_or_create.
update_or_create routes both branches through the model, so the two behave identically: __fillable__ filters update_data whether the row existed or not, and the update path fires the full updating / saving / updated / saved cycle just as the create path fires creating / saving / created / saved.
A column missing from __fillable__ is silently dropped from the update branch. To write a guarded column, add it to __fillable__ or assign it and save() explicitly.
The same filter is available on its own as fill(), which assigns mass-assignable keys without persisting:
user.fill(request_data).save()Updating via queries
Bulk updates go through the query builder and return the affected row count:
count = User.where("active", False).update({"active": True})
count = await User.where("active", False).aupdate({"active": True})Deleting Models
user = User.find(1)
user.delete() # deletes this row, returns True/False
await user.adelete()
User.where("active", False).delete() # bulk delete via the query builderdelete() returns False when the instance has no primary key value or a deleting hook cancels the operation.
Inspecting Instances
user = User.find(1)
user.is_dirty() # any attribute changed since the last load or write?
user.is_dirty("name") # specific attribute(s) changed?
user.is_clean()
user.get_original("name") # value as last read from / written to the database
fresh = user.fresh() # re-fetch this row (returns a new instance)
copy = user.replicate() # clone without the primary key or timestampsAsync: afresh().
The dirty baseline is re-snapshotted whenever the instance is persisted, so create() and save() both leave it consistent with what is actually stored:
user = User.create({"name": "Ada", "email": "ada@example.com"})
user.is_dirty() # False — freshly persisted
user.get_original("name") # "Ada"
user.name = "Ada L."
user.is_dirty("name") # True
user.get_original("name") # "Ada"
user.save()
user.is_dirty() # False again
user.get_original("name") # "Ada L." — the persisted valueThis is what makes audit trails, change logs, and "did this field actually change?" guards correct when they run after a write.
Attribute Casting
__casts__ converts between database values and Python types transparently — on read, and on every write path (create, insert, save):
class Event(Model):
__tablename__ = "events"
__fillable__ = ["title", "payload", "started_at"]
__casts__ = {
"payload": "json",
"started_at": "datetime",
}
event = Event.create({"payload": {"kind": "signup"}, "started_at": "2026-07-28T10:00:00"})
event.payload["kind"] # "signup" — dict, not a string
event.started_at.year # 2026 — datetime, parsed from the ISO stringSupported cast types: "json" / "list" / "dict", "integer" / "int", "float", "decimal", "boolean" / "bool", "string" / "str", "datetime", "date".
A value a cast cannot handle raises a ValueError naming the cast, the value and the column — Cannot cast 'abc' to decimal for column 'amount'. — on both the read and the write side, rather than a driver-level error you had no reason to catch.
The "decimal" cast returns decimal.Decimal values on read and writes exact decimals to the database — the natural pairing for NUMERIC money and rate columns. Floats are converted through their string form, so 0.1 becomes Decimal("0.1") rather than the binary-float expansion.
JSON casts are column-aware: on a native JSON column the dict/list is passed through untouched (what async drivers expect), while on a TEXT column it is serialized to a JSON string. You get the right behavior either way.
Mutating cast values in place
Reading a cast attribute hands back the same decoded object every time, so containers can be edited in place — the edit is visible on the next read, is picked up by dirty checking, and is written by save():
event = Event.find(1)
event.payload["kind"] = "login" # in-place edit of the decoded dict
event.payload # {"kind": "login"} — same object, not a fresh decode
event.is_dirty("payload") # True
event.save() # persisted
Event.find(1).payload # {"kind": "login"}Assigning a whole new value works the same way, on both the sync and async paths:
event.payload = {"kind": "signup", "source": "web"}
event.save() # write-cast applied here, not only in create()
await event.asave()The dirty baseline stores cast containers by value, not by reference, which is what lets an in-place edit register as a change. Writing a new value to the column replaces the decoded object, so a reference you captured before the assignment goes stale — read the attribute again after assigning or saving.
Serialization
to_dict() and to_json() turn an instance into plain data, honoring __hidden__, __visible__, and __appends__:
class User(Model):
__tablename__ = "users"
__hidden__ = ["password_hash"]
__appends__ = ["display_name"]
@property
def display_name(self) -> str:
return self.name.title()
user.to_dict() # {"id": 1, "name": ..., "display_name": ...} — no password_hash
user.to_json() # JSON string; datetimes serialized in UTC with a Z suffix__hidden__— columns excluded from the output.__visible__— when non-empty, an allowlist: only these columns appear.__appends__— extra computed properties included in the output.
Relation aggregates loaded via with_count() and friends (e.g. posts_count) are included alongside the columns, subject to the same __hidden__ / __visible__ filtering.
Relationships
Declare relationships in the __relationships__ dict using the four relationship types from alata.db:
from alata.db import Model, HasOne, HasMany, BelongsTo, BelongsToMany
class User(Model):
__tablename__ = "users"
__relationships__ = {
"profile": HasOne("Profile", "user_id"),
"posts": HasMany("Post", "user_id"),
"roles": BelongsToMany("Role", "role_user", "user_id", "role_id"),
}
class Post(Model):
__tablename__ = "posts"
__relationships__ = {
"user": BelongsTo("User", "user_id"),
}The related model can be given as the class itself or as a string name ("Post"). String names are resolved lazily from the model registry on first use, so forward references and circular model imports just work.
Relationship types
| Type | Signature | Meaning |
|---|---|---|
HasOne(related, foreign_key, local_key="id") | one child | User has one Profile; profiles.user_id points back |
HasMany(related, foreign_key, local_key="id") | many children | User has many Posts; returns a Collection |
BelongsTo(related, foreign_key, local_key="id") | inverse | Post.user_id points at users.id; foreign_key lives on this model |
BelongsToMany(related, pivot_table, foreign_pivot_key, related_pivot_key, local_key="id", related_key="id") | many-to-many | joined through a pivot table; returns a Collection |
Lazy loading
Accessing a relationship attribute runs the query on first access and caches the result on the instance:
user = User.find(1)
user.posts # SELECT ... WHERE user_id = 1 (first access)
user.posts # cached — no second query
post = Post.find(1)
post.user # the owning User, or NoneEager loading
Loading a relationship inside a loop causes one query per row — the classic N+1 problem. with_() loads a relationship for a whole result set in one extra query:
users = User.with_("posts", "profile").where("active", True).get()
for user in users:
user.posts # already loaded — zero extra queriesNested relationships use dot notation, and eager loads work with async retrieval too:
users = User.with_("posts.user").get() # posts, and each post's user
users = await User.with_("posts").aget()Eager-loaded many-relationships are Collections — the same type lazy access returns, so collection helpers chain no matter how the relation was loaded. A parent with no related rows gets an empty Collection, never None or a bare list.
Constrained eager loading
with_() also accepts dict entries mapping a relation name to a callable. The callable receives the related model's query builder — already scoped to the parents — and can filter or order the eager sub-query:
users = User.with_({"posts": lambda q: q.where("status", "published")}).get()
users = User.with_({"posts": lambda q: q.latest()}, "profile").get() # mix with plain namesFor a dotted name, the constraint applies to the final segment — {"posts.comments": cb} constrains the comments query, not the posts query. Constrain both levels by keying each path: with_({"posts": cb1, "posts.comments": cb2}).
limit() inside a constraint caps the whole eager query, not rows per parent. The eager sub-query fetches related rows for all parents at once, so with_({"posts": lambda q: q.latest().limit(3)}) returns three posts total across the whole result set — not three per user. For "one related row per parent", use latest_of_many() / oldest_of_many() instead.
Loading relations after the fact
Already fetched the models? Collection.load() batch-loads relations onto the results with the same one-query-per-relation machinery as with_(), and accepts the exact same entries — dotted names and constraint dicts included:
users = User.where("active", True).get()
users.load("posts", "profile") # one query per relation
users.load({"posts": lambda q: q.where("status", "published")})
if include_comments:
users.load_missing("posts.comments") # only loads what's absentload() re-queries a relation even when it is already loaded (a refresh); load_missing() leaves models that already have the relation untouched and queries only the ones missing it. Both return the collection, and their async twins are aload / aload_missing. See the Collections reference.
A collection holding more than one model class is grouped by class and loaded per class — one query per relation per class. Every item resolves the relation through its own class, so a name that means different things on different models loads correctly, and a class that doesn't define the relation raises ValueError naming that class.
Counting related rows: with_count
with_count() loads a {relation}_count integer attribute onto each result — one grouped subquery per relation regardless of result size, no N+1, for every relationship type including many-to-many:
users = User.with_count("posts").get()
users[0].posts_count # int — 0 when there are no posts
posts = Post.with_count("user", "tags").get() # several relations at onceDict entries constrain what gets counted:
users = User.with_count({"posts": lambda q: q.where("status", "published")}).get()with_sum, with_max, with_min, and with_avg aggregate a related column the same way, loading a {relation}_{fn}_{column} attribute — an optional third argument adds a constraint callable:
users = User.with_sum("orders", "amount").get()
users[0].orders_sum_amount # sum, or None when there are no related rowsParents with no related rows get 0 for counts and None for the other aggregates (SQL aggregates over zero rows). The related model's global scopes — soft deletes included — apply, so excluded rows are never counted. Loaded aggregates appear in to_dict() / to_json() output, subject to the same __hidden__ / __visible__ filtering as columns. Nested (dotted) relation names are not supported for aggregates — eager-load the nested relation and aggregate on the related model instead.
One row from a has-many: latest_of_many and oldest_of_many
A thread has many messages, but list screens usually want just the latest one per thread. latest_of_many() reduces a HasMany to a single-row relationship that behaves like a HasOne:
class Thread(Model):
__tablename__ = "threads"
__relationships__ = {
"messages": HasMany("Message", "thread_id"),
"latest_message": HasMany("Message", "thread_id").latest_of_many(),
}
threads = Thread.with_("latest_message").get() # ONE extra query for all threads
for thread in threads:
thread.latest_message # newest Message, or NoneEager loading resolves every parent's row in a single query using a row_number() window partitioned by the foreign key — portable across PostgreSQL, MySQL 8.0+ (MariaDB 10.2+), and SQLite 3.25+. Lazy access (thread.latest_message) runs a plain ORDER BY ... LIMIT 1 query instead.
Both variants take the ordering column (default "created_at"); ties are broken by the primary key. A row whose ordering column is NULL never wins the reduction — it sorts last in both directions, identically on every dialect, so a missing timestamp can't masquerade as the newest row. (A parent whose related rows are all NULL still resolves to one row, chosen by the primary-key tie-break.)
oldest_of_many() is the mirror — the minimum instead of the maximum:
__relationships__ = {
"first_message": HasMany("Message", "thread_id").oldest_of_many(),
"top_message": HasMany("Message", "thread_id").latest_of_many("score"),
}In a constrained eager load, an of-many relation accepts filter constraints only — ordering or limit() from the constraint are ignored, because the reduction defines its own ordering to pick each parent's single row.
Working with pivot tables
Many-to-many links live in the pivot table, and the pivot mutations live on the relationship — bound to an instance via relation():
user = User.find(1)
user.relation("roles").attach(3) # link role 3
user.relation("roles").attach([4, 5], {"level": 1}) # link several, with extra pivot values
user.relation("roles").detach(3) # unlink role 3 — returns deleted count
user.relation("roles").detach() # unlink everythingsync() makes the pivot rows match a list exactly — attaching what's missing, detaching what isn't listed — and reports what changed:
changes = user.relation("roles").sync([1, 2, 3])
# {"attached": [3], "detached": [7], "updated": []}
user.relation("roles").sync([1, 2], detaching=False) # attach-only, never detaches
user.relation("roles").sync({1: {"level": 9}}) # dict form: id → extra pivot valuestoggle() flips each id — attached ones are detached and vice versa — and update_existing_pivot() updates extra columns on one existing pivot row:
user.relation("roles").toggle([2, 4]) # {"attached": [...], "detached": [...]}
user.relation("roles").update_existing_pivot(3, {"level": 2}) # returns updated row countEvery operation has an async twin: aattach, adetach, atoggle, aupdate_existing_pivot — and async_ for sync, because the literal name async is a reserved word, so it takes a trailing underscore just like with_:
await user.relation("roles").async_([1, 2, 3])A few things to know:
attach()never de-duplicates. It is a plain INSERT with no existence check, so attaching an already-attached id inserts a second pivot row (and passing the same id twice in one call inserts it twice). Reach forsync()— which attaches only what is missing — when idempotency matters, and add a unique constraint on the two pivot key columns if duplicates must be impossible.- Duplicate pivot rows collapse on read: lazy and eager loads both return one related model per distinct id, carrying the first pivot row's data. The extra rows still exist in the table, though, and
with_count()counts them. sync()andtoggle()de-duplicate their input ids, sotoggle([5, 5])acts on id5once. Both run their read and every resulting insert/delete/update inside a single transaction, so a failure part-way through leaves the pivot table exactly as it was. The transaction does not lock out a concurrentsync()on the same parent — that is what the unique constraint is for.- Pivot operations require a saved parent; on an instance without a primary-key value they raise
ValueError. relation()works on every relationship type as a fresh, uncached query path —user.relation("posts").get()always hits the database, in contrast to the cached attribute accessuser.posts.
Extra pivot columns: with_pivot
By default only the two pivot keys travel with the relationship. with_pivot() loads extra pivot-table columns and exposes them on each related model as a pivot dict:
class User(Model):
__tablename__ = "users"
__relationships__ = {
"roles": BelongsToMany("Role", "role_user", "user_id", "role_id").with_pivot("level"),
}
user.roles[0].pivot # {"user_id": 1, "role_id": 3, "level": 2}The pivot dict is populated on both lazy and eager loads. with_pivot() returns a modified copy of the relationship, so it composes at declaration time without mutating a shared definition.
The related model must not map an attribute of its own named pivot. Loading a with_pivot() relationship whose target already has a pivot column raises ValueError instead of writing the pivot dict over real data — rename the column, or drop with_pivot() from that relationship.
Querying by relationship existence
User.has("posts").get() # users with at least one post
User.has("posts", ">=", 5).get() # users with five or more
User.where_has("posts", lambda q: q.where("status", "published")).get()Strict mode
In busy applications, an accidental lazy load inside a loop is an easy way to melt the database. Strict mode turns lazy loads into errors so N+1s surface in development instead of production:
Model.should_be_strict(True) # usually driven by DB_CONFIG["strict_mode"]
user = User.find(1)
user.posts # raises LazyLoadingViolationError — use with_("posts") insteadThe scaffolded config/database.py wires DB_CONFIG["strict_mode"] to a settings flag, so you can enable it per environment.
Query Scopes
Local scopes
Package a reusable query constraint as a method with the @scope decorator. The method receives the model class and a fresh query builder, and becomes callable as a class method:
from alata.db import Model, scope
class Post(Model):
__tablename__ = "posts"
@scope
def published(cls, query):
return query.where("status", "published")
Post.published().order_by("created_at", "desc").get()Scope methods receive the model class as their first argument, not an instance — name the parameter cls. Scopes take no additional arguments; they are zero-argument query entry points.
Global scopes
A global scope applies to every query on the model automatically. Subclass GlobalScope and return a list of filter conditions:
from alata.db import Model, GlobalScope
class TenantScope(GlobalScope):
def apply(self, query, model_class):
return [model_class.tenant_id == current_tenant_id()]
class Invoice(Model):
__tablename__ = "invoices"
__global_scopes__ = {"tenant": TenantScope()}Bypass scopes per query on the query builder:
Invoice.where("total", ">", 0).without_global_scope("tenant").get()
Invoice.where("total", ">", 0).without_global_scopes().get() # bypass ALL scopesGlobal scopes declared on mixins and parent classes merge automatically — this is exactly how soft deletes work.
Lifecycle Events
Models fire hooks around every write. Register them with the @hook decorator:
from alata.db import Model, hook
class User(Model):
__tablename__ = "users"
@hook("creating")
def set_defaults(self):
if not self.status:
self.status = "active"
@hook("deleted")
def cleanup(self):
...The eight events, in firing order:
| Operation | Before | After |
|---|---|---|
Insert (create / save on new instance) | creating, saving | created, saved |
Update (save on existing instance) | updating, saving | updated, saved |
Delete (delete) | deleting | deleted |
A before hook (creating, updating, saving, deleting) can cancel the operation by returning False — the write never reaches the database:
@hook("deleting")
def protect_admins(self):
if self.is_admin:
return False # delete() returns False, row survivesHooks are collected across the whole class hierarchy, so mixins can contribute hooks too.
Async hooks
A hook may be an async def. Async hooks are awaited by the async persistence methods — acreate, asave, adelete and their _quietly variants — in registration order, so an async before hook can cancel the write by returning False exactly like a synchronous one:
class Document(Model):
__tablename__ = "documents"
@hook("saving")
async def validate_remotely(self):
if not await policy.allows(self):
return False # cancels the write
@hook("saved")
async def reindex(self):
await search.index(self)
await Document.acreate({...}) # both hooks runReaching an async hook from a synchronous write raises AsyncHookError, naming the hook and the async method to use instead — the coroutine is never quietly discarded:
from alata.db.events import AsyncHookError
Document.create({...})
# AsyncHookError: Hook 'reindex' on Document is an async function, but the 'saved'
# event fired from a synchronous persistence call. … call acreate() or asave() instead …So: if a model has async hooks, write to it through the async twins. A model can freely mix sync and async hooks — the synchronous path only fails when it actually reaches an async one.
@hook also rejects misuse at decoration time: decorating a non-callable, or an async generator function (async def with yield), raises TypeError immediately rather than registering a hook that could never run.
Bulk operations that skip instantiating models — insert(), upsert(), and query-builder update()/delete() — fire no model events: no hooks run and nothing is dispatched to the event bus. Hooks fire for create(), save(), and instance delete() (and their async variants).
Quiet writes: skipping events on purpose
For backfills, imports, and hooks that must persist their own model without re-triggering the event cycle, every instance write has a quiet variant. It behaves identically — casts, fillable filtering, soft deletes all apply — but no hooks run and nothing reaches the event bus:
user = User.create_quietly({"name": "Imported", "email": "import@example.com"})
user.name = "Renamed"
user.save_quietly()
user.delete_quietly() # a soft-deletable model still soft-deletes — just silentlyAsync: acreate_quietly, asave_quietly, adelete_quietly.
Model events on the event bus
After-events (created, updated, saved, deleted) additionally dispatch a ModelEvent through the application event bus, fire-and-forget on the running event loop. Listen for it to react to any model change in one place:
from alata.events.model_event import ModelEvent
# a ModelEvent carries:
event.model # the model instance
event.event_name # "created" | "updated" | "saved" | "deleted"
event.model_class # e.g. "User"
event.table_name # e.g. "users"Dispatch is skipped silently when no event loop is running (for example inside sync CLI commands) or when the container isn't booted, and listener exceptions are routed to the logging system rather than swallowed.
Soft Deletes
Add the SoftDeletes mixin before Model in the base-class list, and create a deleted_at column in your migration (the blueprint's soft_deletes() does it):
from alata.db import Model, SoftDeletes
class Post(SoftDeletes, Model):
__tablename__ = "posts"Now delete() sets deleted_at instead of removing the row, and every query on the model automatically excludes trashed rows (via a built-in soft_deletes global scope):
post.delete() # sets deleted_at — row remains in the table
post.trashed # True
Post.all() # trashed rows excluded automatically
Post.where("id", post.id).with_trashed().first() # include trashed
Post.where_not_null("id").only_trashed().get() # ONLY trashedRestoring and permanently deleting:
post.restore_instance() # clears deleted_at on this instance
post.force_delete() # actually removes the row
# via the query builder, in bulk:
Post.where("user_id", 7).only_trashed().restore() # returns affected count
Post.where("user_id", 7).soft_delete() # bulk soft deleteBulk soft_delete() respects the soft-delete scope, so rows already in the trash are skipped — their original deleted_at survives and the returned count is what this call actually deleted. Chain with_trashed() to re-stamp them. restore() bypasses the scope, as it must.
Async variants exist throughout: adelete, asoft_delete, arestore_instance, aforce_delete, and arestore / asoft_delete on the query builder.
with_trashed() and only_trashed() live on the query builder, so chain them after a query starter such as where(...) or where_not_null("id") — they are not class methods on the model.
Multiple Connections
Point a model at any named connection from config/database.py with __connection__:
class AuditLog(Model):
__tablename__ = "audit_logs"
__connection__ = "analytics"All reads and writes for the model — sync and async — route through that connection. Anything the model has to reflect follows it: many-to-many pivot tables, where_has() subqueries on those relationships, and join() against a table no model maps are all read from the model's own connection, never from default. See Getting Started for defining connections.
Reflection is synchronous, so a connection used for many-to-many relationships needs a sync engine even in an async-only deployment — otherwise the first pivot operation raises TableReflectionError naming that connection. Reflected tables are then cached per connection and table; after a migration reshapes one, call DB._clear_table_cache() in the same process.