Skip to content

How to use several databases in one app

This how-to connects one process to two databases, an application database and an analytics database, and routes each call to the right one.

Give each connection a name, mark one as the default, and route calls to each:

await connect("sqlite::memory:", name="app", default=True, auto_migrate=True)
await connect("sqlite::memory:", name="analytics", auto_migrate=True)

async with engines.session():
    await Metric.create(name="signups", value=12)  # the default, "app"
    app_metrics = await Metric.all()

await Metric.using("analytics").create(name="page_views", value=1840)
analytics_metrics = await Metric.using("analytics").all()

print(len(app_metrics), len(analytics_metrics))

It prints:

1 1

Each database holds one metric: signups went to the default connection, app, through the session, and page_views went to analytics through .using(). Named connections in the Connections guide explains how naming and the default work, and Sessions explains the session.

The examples use one model, a named metric:

from ferro import Field, Model


class Metric(Model):
    id: int | None = Field(default=None, primary_key=True)
    name: str
    value: int = 0
from ferro import Field, Model


class Metric(Model):
    id: Annotated[int | None, Field(primary_key=True)] = None
    name: str
    value: int = 0

Each connect() call opens its own pool, sized with pool=PoolConfig(...), and auto_migrate=True creates the metric table on each database.

Route calls to a connection

Every call needs a route. Inside async with engines.session():, unqualified calls use the session's connection: the default connection when you open the session with no name. Model.using(name) routes one call and needs no session.

To run a whole block on one named connection, open a session on it:

async with engines.session("analytics") as session:
    busy = (
        await session.query(Metric).where(lambda metric: metric.value > 100).all()
    )

print([metric.name for metric in busy])

It prints:

['page_views']

Only the analytics database has a metric above 100.

Inside a session, .using() must name the session's connection. Naming another one raises ValueError rather than running the call outside the session:

async with engines.session():
    try:
        await Metric.using("analytics").all()
    except ValueError as error:
        print(f"{type(error).__name__}: {error}")
    else:
        raise AssertionError("expected ValueError")

It prints:

ValueError: Explicit `using='analytics'` conflicts with the ambient session bound to connection 'app'. Pass an explicit `session=` for that connection, or open a session on 'analytics'.

A call with no session and no using= raises RuntimeError; Ferro never falls back to the default connection on its own.

Routing is per call: .using() changes no global state, so two coroutines can use different databases at once. A .using() call outside a session has no identity map, so every load returns a new instance; see Sessionless operations.

Transactions on a named connection

transaction(using=name) pins a transaction to one connection. Unqualified calls inside the block run on it, and routing a call elsewhere raises ValueError:

async with transaction(using="analytics"):
    await Metric.create(name="clicks", value=42)  # runs on "analytics"
    try:
        await Metric.using("app").create(name="clicks", value=42)
    except ValueError as error:
        print(f"{type(error).__name__}: {error}")
    else:
        raise AssertionError("expected ValueError")

print(len(await Metric.using("analytics").all()))

It prints:

ValueError: Operations inside a transaction inherit the transaction connection
2

The clicks metric landed on analytics, which now holds two metrics; the call routed to app was refused. Raw SQL through execute() and fetch_all(), and nested transaction() blocks, inherit the connection too. Ferro has no distributed transactions: one transaction spans one connection, so writes to two databases never commit together.

Create tables on one connection

connect(url, name=..., auto_migrate=True) creates the tables on that connection as it connects. To create them later, call create_tables(using=name):

await connect("sqlite::memory:", name="archive")
report = await create_tables(using="archive")

print([statement.subject for statement in report.statements])

It prints:

['metric']

create_tables() returns a PassReport of the statements it ran, here one CREATE TABLE for metric. Two connections that name the same database share its run lock, so their passes run one after the other (Concurrent passes and the run lock).

For production schema changes, use migrations: a project whose models live in separate databases configures each one with its own migrations directory, and every command names it with --database (Deploying migrations).

Security and routing rules

Never route from untrusted input

Choose connection names from constants or trusted server-side logic. A name taken from a request parameter or header lets the caller pick the database.

  • Typical roles. A read replica for expensive list endpoints (Metric.using("replica")), an analytics database that receives event rows, or a database another team owns.
  • Keep credentials server-side. Elevated service-role connections belong in configuration, not source control, and a user-facing runtime never makes one its default.
  • Pools isolate roles, not request context. A named connection separates credentials and pooling; it does not give each request its own row-security context inside one shared pool. Objects loaded through an elevated connection can hold elevated data, so filter them before returning them to users.
  • No automatic routing. Ferro has no read/write splitting, cross-connection joins or two-phase commit. Every route is a using=, session= or engines.session(name) you write yourself.

See also