Data model
Scholar Lens keeps everything in one Postgres database with the pgvector extension: accounts, the Library and its vectors, Spaces, chats, runs and their events, jobs, study material and settings. This page lists the tables and how they relate. The models are in app/db/models.py and app/db/study.py; the migrations in app/db/migrations/versions/.
The main tables
Figure 1 shows the core of the schema.
erDiagram
users ||--o{ documents : owns
users ||--o{ spaces : owns
spaces ||--o{ space_sources : has
documents ||--o{ space_sources : "used by"
spaces ||--o{ space_members : "shared with"
users ||--o{ space_members : joins
documents ||--o{ doc_nodes : "section tree"
documents ||--o{ chunks : "indexed as"
doc_nodes ||--o{ chunks : contains
spaces ||--o{ sessions : "chats in"
sessions ||--o{ messages : holds
sessions ||--o{ executions : runs
executions ||--o{ execution_events : logs
sessions ||--o{ session_files : attaches
session_files ||--o{ chunks : "indexed as"
sessions ||--o{ code_cells : runs
Figure 1. The core tables. Study, memory, access and admin tables are listed below.
Accounts and access
| Table | What it holds |
|---|---|
users | Username, argon2id password hash, role (admin or user), active, pending approval, must change password, preferences |
auth_sessions | One row per signed-in browser: the hashed refresh token, user agent, IP, expiry, revocation |
api_tokens | Personal access tokens for the extension: name, SHA-256 hash, prefix, last used, revoked |
invites | Space invite links: hashed token, role, expiry |
space_members | Who a Space is shared with, as editor or viewer |
access_grants | Sealed cookies from the sign-in browser, per student, Space and site |
audit_log | Security-relevant actions: sign-ins, token creation, admin access to content, publishing |
Library
| Table | What it holds |
|---|---|
documents | A Library item: kind (file, url, youtube, arxiv, doi, page), title, authors, year, bibliographic fields (bib), table of contents, page labels, file path and hash, status, chunk count, index_version, embedding model, shared flag |
doc_nodes | The section tree: number, title, level, pages, parent, and the section summary with its embedding |
document_links | An item brought in from a saved page (reference, file, web page, video) and the page; searched with the page |
duplicate_pairs | A new Library item that looks like one already there: the two items, score, basis (file, DOI, title, content...), a few lines on how they differ, and the decision (open, kept, or replace once indexed) |
chunks | Searchable text: the owner, the document or session file it belongs to, its section node, scope (library, session, note), index version, page, section path, text, halfvec embedding and generated tsvector |
publish_requests | A user's request to put an item in the shared Library, with notes and the admin's decision |
A document's status is processing, ready, failed or reference (a citation with no text, from the extension). Only chunks with the document's current index_version are searched.
Indexes: HNSW (halfvec_cosine_ops) on chunks.embedding, GIN on chunks.tsv.
Spaces and chats
| Table | What it holds |
|---|---|
spaces | Title, icon, instructions, default style, web policy (off, auto, always), code policy (off, ask, auto) |
space_sources | A document in a Space: its scope (the label as typed, the sections and the page ranges it resolved to), role, pinned |
sessions | A chat in a Space, with the message its current branch ends at (active_message_id) |
messages | User and assistant messages. parent_id makes them a tree, so edits and "ask again" are branches. An answer's citations, confidence and mind map are in its payload. |
session_files | Files attached to one chat |
memory_items | Memory about the user or a Space, pinned by the student or proposed by the agent and accepted |
Runs and jobs
| Table | What it holds |
|---|---|
executions | A run: status, idempotency key, heartbeat, times, cancel flag, resume count, per-run options |
execution_events | The run's numbered event log, for the live view and replay |
checkpoints, checkpoint_blobs, checkpoint_writes, checkpoint_migrations | LangGraph's own tables for durable runs. LangGraph creates and migrates them; Alembic leaves them alone. |
jobs | Background jobs: kind, status, priority, attempts, run-after time, progress |
code_cells | Code run in a chat: source, origin (answer or student), status, output streams, plots and files, error, duration, linked run |
Web
| Table | What it holds |
|---|---|
web_cache | Search results per provider and query, for a day |
web_pages | Fetched pages and their outcome (including refusals), for a week |
usage | Tavily credits per user per month |
Study
| Table | What it holds |
|---|---|
study_sets | A flashcard set, quiz, glossary or concept map, with its scope and status |
study_items | Cards, questions, terms, concepts and links, each with its citation and quote check |
quiz_attempts | A student's answers and marks |
notes | Highlights and notes on a document page, with their position. Each note is also a chunks row with scope note, so answers can find it. |
Settings and metadata
| Table | What it holds |
|---|---|
app_settings | Settings saved by an admin. Secrets are encrypted. |
app_meta | A key-value table from the baseline schema, kept for small facts about the installation. Unused today. |
Conventions
- Primary keys are UUID strings, except log tables with big-integer ids.
- Times are stored with time zones, in UTC.
- Deleting a user, Space, document or chat cascades to what belongs to it.
- Every schema change is a hand-written Alembic migration. A test (
alembic check) fails if the models and the migrations disagree. - The embedding size is fixed when the database is created (
EMBEDDING_DIMENSIONS).