Relationships
Foreign keys, cascade rules, and join patterns across the 9-table Drizzle schema.
4 min read
Foreign key inventory
Every foreign key in packages/db/src/schema.ts, exactly as declared:
| From table | Column | References | On delete |
|---|---|---|---|
sessions | user_id | users.id | CASCADE |
sessions | agent_id | agents.id | (no action — default RESTRICT) |
session_messages | session_id | sessions.id | CASCADE |
memory_entries | session_id | sessions.id | SET NULL |
memory_entries | user_id | users.id | CASCADE |
tool_executions | session_id | sessions.id | CASCADE |
agent_skills | agent_id | agents.id | CASCADE |
agent_skills | skill_id | skills.id | CASCADE |
personas has no foreign keys in or out.
Cascade behavior, explained
Deleting a user cascades hard
DELETE FROM users WHERE id = '...'This cascades to:
- All of that user's
sessions(viasessions.user_id ON DELETE CASCADE) - Which cascades to all
session_messagesandtool_executionsfor those sessions - All of that user's
memory_entriesdirectly (viamemory_entries.user_id ON DELETE CASCADE)
Deleting a user is destructive and total — there's no soft-delete or archival step at the schema level. If you need to preserve conversation history after account deletion, add that logic in application code before the delete, not after.
Deleting a session is destructive to messages/tools, but not memory
DELETE FROM sessions WHERE id = '...'session_messagesfor that session: deleted (CASCADE)tool_executionsfor that session: deleted (CASCADE)memory_entriesthat reference that session: kept, butsession_idis set toNULL(SET NULL)
This is intentional: a memory entry (a fact learned, a summary stored) is meant to outlive the session that produced it. The transcript and tool audit log are considered session-scoped and disposable; memory is considered durable.
Deleting an agent does NOT cascade
sessions.agent_id REFERENCES agents.id has no onDelete clause, which means Postgres defaults to NO ACTION (effectively RESTRICT). Attempting to delete an agents row that has any sessions referencing it will fail with a foreign key violation. In practice, agents aren't expected to be deleted through this table at all — agent definitions are normally managed as files in workspace/agents/ (Level 1) and only mirrored into Postgres for Level 3 lookups.
Deleting a skill or agent cascades through the join table only
agent_skills cascades on both sides — deleting either an agent or a skill removes the corresponding join rows, but (per above) deleting an agent will itself be blocked first if sessions reference it.
Relationship cardinality summary
Why no relation to the filesystem session model?
The Postgres sessions table and the runtime's StoredSession (used by SessionStore, ChannelSessionBridge, and the Channel Hub — see Channel Adapters) are related concepts but not the same object. At Level 1–2, sessions live as JSONL files or SQLite rows managed by @anvio/workspace, independent of this Drizzle schema. The Postgres schema is one possible persistence backend for Level 3, wired in by choosing storage.provider / memory.provider in workspace/anvio.yaml — it is not automatically the source of truth just because it exists in the monorepo.
Querying relationships with Drizzle
Example: fetch a session with its messages and the agent it ran as, using the relations declared above (via drizzle-orm's query builder — see packages/db/src/index.ts for the createDb() factory that wires schema into the client):
import { eq } from 'drizzle-orm';
import { createDb, sessions, sessionMessages, agents } from '@anvio/db';
const db = createDb(process.env.DATABASE_URL!);
const [session] = await db.select().from(sessions).where(eq(sessions.id, sessionId));
const messages = await db
.select()
.from(sessionMessages)
.where(eq(sessionMessages.sessionId, sessionId));
const [agent] = await db.select().from(agents).where(eq(agents.id, session.agentId));TODO: verify against
packages/db/src— no Drizzlerelations()helper calls were found inschema.ts(only rawpgTable+.references()). If you needdb.query.sessions.findMany({ with: { messages: true } })-style relational queries, you'll need to addrelations()definitions yourself; they don't exist yet.
Last updated on