Lesson 4: Database (ERD)

Goal of this lesson

Describe the main tables, explain the three declared foreign keys, and know why most relationships are “logical” instead of enforced by the database.

Quick facts

FactValue
DatabaseOne SQLite file, lafina.db, on the phone (OP-SQLite)
Schema version28 (versioned migrations)
Tables36 application tables (with FTS5), excluding SQLite internals
Declared foreign keys3
Sync entity types7: profile, task, event, time_block, reminder, note, custom_category
Guest user IDlocal_guest_user
Android only

This ERD is the Android SQLite schema. The backend’s Neon PostgreSQL database is a separate schema.

Personal planning tables

users Local and cloud identities
  • id PK
  • username
  • email
  • role
  • cloud_account_id
user_preferences One row per user
  • id PK
  • user_id FK → users.id, UNIQUE, cascade
  • wake_time
  • sleep_time
  • reminder_lead_minutes
tasks To-dos and deadlines
  • id PK
  • user_id
  • title
  • due_date
  • due_time
  • is_completed
  • recurrence_rule
events Calendar events and classes
  • id PK
  • user_id
  • title
  • date
  • start_time
  • end_time
time_blocks Planned focus or study time
  • id PK
  • user_id
  • title
  • date
  • start_time
  • end_time
reminders Things LAFINA calls you about
  • id PK
  • user_id
  • task reminder TEXT, not a task ID
  • scheduled_at
  • trigger_at
  • status pending / triggered / snoozed / acknowledged / missed
notes Markdown notes
  • id PK
  • user_id
  • title
  • body
  • is_pinned
  • category
custom_categories User-made labels
  • id PK
  • user_id
  • name
  • color
PK = primary key. FK = declared SQL foreign key. Every planner table has a user_id that points to users logically (not enforced).

Conversations, recordings, and alarms

chat_sessions One chat thread
  • id PK
  • user_id
  • title
  • generation
messages Messages in a thread
  • id PK
  • session_id FK → chat_sessions.id, cascade
  • sender
  • content
  • seq
  • metadata_json review cards, sources
chat_memories Things LAFINA remembers
  • id PK
  • user_id
  • text
  • session_id
recorded_meetings Lecture/meeting recordings
  • id PK
  • user_id FK → users.id, no cascade
  • title
  • status
  • transcript_json
  • notes_json
alarms Device-local alarm clocks
  • id PK
  • user_id
  • hour
  • minute
  • repeat_days
  • next_trigger_at
  • source_reminder_id

The three declared foreign keys

Foreign keyOn delete
user_preferences.user_id → users.idCascade
messages.session_id → chat_sessions.idCascade (delete a chat → its messages go too)
recorded_meetings.user_id → users.idNo cascade

Why are most relationships 'logical' only?

Planner tables point to users by user_id, but the app enforces that link in code instead of with a database foreign key. Reasons:

  • Records arrive from sync and must be applied safely even if related rows arrive in a different order.
  • Delivery jobs and events must be able to outlive their reminder for audit and recovery.
  • reminders.task stores text, not a task ID, so a reminder can exist without a task.

All 36 tables, grouped

GroupTables
Identity and preferences (5)users, user_preferences, active_session, remember_me, local_settings
Personal planning (6)tasks, events, time_blocks, reminders, notes, custom_categories
Conversations (4)chat_sessions, messages, chat_memories, chat_legacy_quarantine
Alarms and delivery (5)alarms, job_queue_items, reminder_delivery_jobs, reminder_delivery_events, reminder_delivery_styles
Synchronization (6)sync_outbox, sync_metadata, sync_state, sync_control, sync_conflicts, entity_local_revisions
Derived local metadata (3)notes_search_index, event_time_local, attachment_cleanup
Reviewed operations (1)assistant_operation_receipts
Recordings and personalization (3)recorded_meetings, user_behavior_logs, ml_feature_snapshots
Handbook retrieval (3)handbook_meta, handbook_chunks, handbook_chunks_fts
LAFINA entity relationship diagram
Official ERD (generated from the schema). Large: open full size and zoom. Open full size ↗

Check yourself

  • How many tables, which schema version, and how many declared foreign keys?
  • Why is reminders.task text instead of a foreign key to tasks?
  • What happens to messages when a chat session is deleted?
  • Which tables support sync? Which one stores review-card receipts?
  • What is handbook_chunks_fts and why does it exist?