# Identity map

Within a session, Ferro's identity map makes one database row one Python object: fetching a row you already hold returns that same instance, refreshed to the database's current values.

The examples on this page use this `User` model, with four users saved (ada, grace, alan and edsger):

```python
from ferro import Field, Model


class User(Model):
    id: int | None = Field(default=None, primary_key=True)
    username: str = Field(unique=True)
    email: str
    age: int
    active: bool = True
```

```python
from ferro import Field, Model


class User(Model):
    id: Annotated[int | None, Field(primary_key=True)] = None
    username: Annotated[str, Field(unique=True)]
    email: str
    age: int
    active: bool = True
```

Fetching ada twice in one session, with a change to her row in between, gives back one object with the new value:

```python
from ferro import engines, execute

async with engines.session():
    ada = await User.get(1)

    # Something else (another request, a script, raw SQL) changes the
    # row without going through `ada`.
    await execute('UPDATE "user" SET email = ? WHERE id = ?', "ada@lovelace.dev", 1)

    again = await User.get(1)
    assert again is ada  # the same object
    assert ada.email == "ada@lovelace.dev"  # refreshed in place

    # An unsaved edit does not survive a re-fetch: the database wins.
    ada.email = "unsaved edit"
    await User.get(1)
    assert ada.email == "ada@lovelace.dev"
```

The second `User.get(1)` ran a query, found the instance the session already held and updated it in place. The unsaved edit was overwritten the same way: on a re-fetch, the database wins.

## Refresh on load

Every fetch runs a query; the identity map decides only whether its row comes back as a new instance or as the one you hold. It is a table for removing duplicates, not a query cache, so a held instance always shows what the last fetch read.

Ferro does not merge old and new values, because a merge policy is how stale-read bugs survive. If you have local changes you care about, `save()` them before the next fetch of that row, or do not hold a changed, unsaved instance across an `await` that could fetch it again.

## One row, one instance

Without an identity map, two queries that return the same row would give you two separate copies, and a change to one would not show through the other. With it, every way of fetching the row returns the same object. This block runs inside a [session](<https://ferro-orm.x54.sh/guide/connections/#sessions>):

```python
by_key = await User.get(2)
by_filter = await User.where(lambda user: user.username == "grace").first()
assert by_filter is by_key

by_key.email = "grace@navy.mil"
assert by_filter.email == "grace@navy.mil"
```

The change made through `by_key` shows through `by_filter`, because they are one object. The guarantee holds within one session on one connection; it does not reach across processes or across sessions in the same process, since each session has its own map.

## Weak-reference map

The identity map lives in the Rust engine, one per session, keyed by connection, model name and primary key. Each entry is a weak reference to a Python instance, not the instance itself, and two things follow from that:

- **The map never keeps an instance alive.** When your code drops the last reference to an instance, Python releases it like any other object. Its entry is cleaned up on the next lookup that finds it, or by a periodic sweep, and the next fetch of that row builds a new instance.
- **`a is b` across two fetches holds only while you hold the first instance.** Once it is released, there is nothing for the second fetch to return.

Each session's map is its own, so a fetch in one session never returns an instance from another.

## Sessionless operations

The session bounds the map's lifetime, so identity is a session feature. Ferro resolves the connection for every operation once, with three outcomes:

- **A session is active**, from `async with engines.session():` or an explicit `session=`: the operation uses that session's identity map.
- **`using="name"` with no session**: the operation runs against that connection with no identity map. Every load returns a new instance, and nothing is kept, so nothing can go stale.
- **No session and no `using=`**: Ferro raises `RuntimeError` instead of guessing a connection.

These calls run with no session open:

```python
# `using=` alone: no session, so no identity map.
first = await User.using("default").get(2)
second = await User.using("default").get(2)
assert first is not second
assert first.email == second.email == "grace@example.com"

# No session and no `using=`: Ferro has no route for the call.
try:
    await User.get(2)
except RuntimeError as error:
    assert str(error).startswith("No database route for this operation.")
else:
    raise AssertionError("a call with no route must raise RuntimeError")
```

Both `using=` loads read grace's row, as two separate objects. The last call has no route and raises `RuntimeError: No database route for this operation. Open a session (...)`.

A `using=` that names a different connection from the session you are inside raises `ValueError`, so an operation never runs around the session you opened without telling you.

## What enters the map

Inside a session, an instance enters the map whenever Ferro hydrates or saves a full model:

- `Model.get(pk)` and `Model.get_or_none(pk)`
- query results from `.first()` and `.all()`
- `Model.create(...)`
- `instance.save()` and `instance.refresh()`

Three kinds of operation leave the map alone:

- Any of the above with `using=` and no session, as [Sessionless operations](<#sessionless-operations>) describes.
- `Model.bulk_create([...])`, which returns a row count, not instances. Query the rows again if you need instances in the map.
- Raw SQL (`fetch_all`, `fetch_one`), whose rows are plain dicts.

## The unloaded instance

A forward relation that nothing loaded reads as an *unloaded instance*: a real instance of the related model holding only its primary key. Inside a session, Ferro registers it in the map under that key, so it is the row's one object like any other. A later `get()` or query of that row fills the same object in place, and `post.author is await Author.get(author_id)` holds.

This block runs inside a [session](<https://ferro-orm.x54.sh/guide/connections/#sessions>) on `Author` and `Post`, the models the API reference uses. It adds an author and a post, drops both, then fetches the post again, so the session holds no author when `post.author` is first read:

```python
lin = await Author.create(name="Lin Wei", email="lin@example.com")
post = await Post.create(title="Notes on Pascal", author_id=lin.id)
author_id, post_id = lin.id, post.id
del lin, post  # the map keeps no instance alive

post = await Post.get(post_id)
author = post.author  # unloaded: built from author_id, registered under it
print(repr(author))

fetched = await Author.get(author_id)  # fills the registered instance
print(repr(fetched))
assert fetched is author
assert post.author is author
```

```text
Author(id=3, unloaded)
Author(id=3, name='Lin Wei', email='lin@example.com')
```

The first `print` shows the key alone and sent no query. `Author.get()` ran its query, found the unloaded instance under that key and filled it, so `fetched`, `author` and `post.author` are one object. `await post.author.load()` fills it the same way.

Outside a session there is no map to register in, so the unloaded instance is cached on the parent alone: reading `post.author` twice gives the same object, and a separate fetch of the author gives another.

An unloaded instance never serializes as a partial row. Its own `model_dump()` and `model_dump_json()` raise `InstanceNotLoaded`. Any other path goes through Pydantic's serializer (a model that nests it, a `TypeAdapter`, a FastAPI response model), and Pydantic wraps the refusal in its own `PydanticSerializationError`, whose message carries the `InstanceNotLoaded` one:

```python
from pydantic import TypeAdapter
from pydantic_core import PydanticSerializationError

from ferro import InstanceNotLoaded

async with engines.session():  # a session that holds no author yet
    post = await Post.get(1)
    try:
        TypeAdapter(Author).dump_python(post.author)
    except PydanticSerializationError as error:
        assert isinstance(error.__cause__, InstanceNotLoaded)
        assert "await post.author.load()" in str(error)
    else:
        raise AssertionError("expected PydanticSerializationError")
```

In Python mode the `InstanceNotLoaded` is the error's `__cause__`; a JSON dump keeps only the message. Load the relation first, with `include()` on the query or `await post.author.load()`.

`InstanceNotLoaded` is not an `AttributeError`, so nothing mistakes an unloaded column for an absent one. `hasattr(post.author, "name")` and `getattr(post.author, "name", None)` raise it rather than answering `False` or `None`, and a Pydantic model built with `from_attributes=True` fails validation with a `get_attribute_error` whose message carries the refusal, instead of filling `name` with its default. A name that is no column still raises a plain `AttributeError`.

## Eviction

Because a re-fetch already refreshes a held instance, you rarely need to manage the map by hand. Two operations let you do so. These blocks run inside a [session](<https://ferro-orm.x54.sh/guide/connections/#sessions>).

`refresh()` reloads an instance you hold, in place. It is the same mechanism as a re-fetch, as a method:

```python
from ferro import execute

alan = await User.get(3)
await execute('UPDATE "user" SET age = 18 WHERE id = 3')
await alan.refresh()
assert alan.age == 18
```

The `UPDATE` went around `alan`, and `refresh()` read the new age into the same object.

`evict_instance()` removes one entry from the session's map, so the next fetch builds a new instance instead of returning the one you hold. Use it when you want to detach from an instance you still reference:

```python
from ferro import evict_instance

ada = await User.get(1)
evict_instance(User, str(ada.id))
fresh = await User.get(1)
assert fresh is not ada
```

`fresh` is a new object for the same row, and `ada` is now detached from the map. The `pk` argument is the primary key as a string. `evict_instance` resolves its route the way any operation does; pass `using=` or `session=` to target a map other than the current session's.

Ferro also evicts on its own, scoped to the session:

- **A rolled-back transaction** empties the session's whole map. After a rollback, any instance created or changed inside it may no longer match the database, so Ferro starts over rather than guess which ones.
- **A bulk `update()` or `delete()`** evicts that model's entries in the session. Other models' instances stay.

One session's rollback or bulk write never touches another session's map.

> [!NOTE]
>
> **`ferro.migrate()` does not evict**
>
> Running `ferro.migrate()` while a session is open leaves that session's identity map as it is. Sequence schema changes and open sessions yourself.

## Turning the map off

The identity map is on by default. Pass `identity_map=False` to `connect()` to turn it off for every session on that connection:

```python
from ferro import connect, engines

await connect("sqlite::memory:", auto_migrate=True, identity_map=False)

async with engines.session():
    ada = await User.create(username="ada", email="ada@example.com", age=36)
    first = await User.get(ada.id)
    second = await User.get(ada.id)
    assert first is not ada
    assert first is not second  # no map, even inside a session
```

Inside the session, two loads of the same row returned two objects. The map is weak-valued and session-scoped, so turning it off saves the deduplication work, not memory; it suits read-heavy, ETL-style jobs where instance identity means nothing.

## Identity map in tests

A session's map lives as long as the session, so tests that share a process reset connection state between cases. `reset_engine()` closes every connection and any sessions still open on them:

```python
import pytest

from ferro import connect, engines, reset_engine


@pytest.fixture
async def db():
    await connect("sqlite::memory:", auto_migrate=True)
    async with engines.session():
        yield
    reset_engine()
```

Each test that takes `db` starts with an empty database and a session of its own. [Testing](<https://ferro-orm.x54.sh/howto/testing/index.md>) covers fixtures in full.

## See also

- [Architecture](<https://ferro-orm.x54.sh/concepts/architecture/index.md>): where the identity map sits in the engine
- [Performance](<https://ferro-orm.x54.sh/concepts/performance/index.md>): what the map costs and saves
- [Connections › Sessions](<https://ferro-orm.x54.sh/guide/connections/#sessions>): opening and nesting sessions
- `evict_instance`: the reference entry
