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
| Fact | Value |
|---|---|
| Database | One SQLite file, lafina.db, on the phone (OP-SQLite) |
| Schema version | 28 (versioned migrations) |
| Tables | 36 application tables (with FTS5), excluding SQLite internals |
| Declared foreign keys | 3 |
| Sync entity types | 7: profile, task, event, time_block, reminder, note, custom_category |
| Guest user ID | local_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 -
idPK -
username -
email -
role -
cloud_account_id
user_preferences One row per user -
idPK -
user_idFK → users.id, UNIQUE, cascade -
wake_time -
sleep_time -
reminder_lead_minutes
tasks To-dos and deadlines -
idPK -
user_id -
title -
due_date -
due_time -
is_completed -
recurrence_rule
events Calendar events and classes -
idPK -
user_id -
title -
date -
start_time -
end_time
time_blocks Planned focus or study time -
idPK -
user_id -
title -
date -
start_time -
end_time
reminders Things LAFINA calls you about -
idPK -
user_id -
taskreminder TEXT, not a task ID -
scheduled_at -
trigger_at -
statuspending / triggered / snoozed / acknowledged / missed
notes Markdown notes -
idPK -
user_id -
title -
body -
is_pinned -
category
custom_categories User-made labels -
idPK -
user_id -
name -
color
Conversations, recordings, and alarms
chat_sessions One chat thread -
idPK -
user_id -
title -
generation
messages Messages in a thread -
idPK -
session_idFK → chat_sessions.id, cascade -
sender -
content -
seq -
metadata_jsonreview cards, sources
chat_memories Things LAFINA remembers -
idPK -
user_id -
text -
session_id
recorded_meetings Lecture/meeting recordings -
idPK -
user_idFK → users.id, no cascade -
title -
status -
transcript_json -
notes_json
alarms Device-local alarm clocks -
idPK -
user_id -
hour -
minute -
repeat_days -
next_trigger_at -
source_reminder_id
The three declared foreign keys
| Foreign key | On delete |
|---|---|
user_preferences.user_id → users.id | Cascade |
messages.session_id → chat_sessions.id | Cascade (delete a chat → its messages go too) |
recorded_meetings.user_id → users.id | No 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.taskstores text, not a task ID, so a reminder can exist without a task.
All 36 tables, grouped
| Group | Tables |
|---|---|
| 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 |
Check yourself
- How many tables, which schema version, and how many declared foreign keys?
- Why is
reminders.tasktext instead of a foreign key totasks? - What happens to messages when a chat session is deleted?
- Which tables support sync? Which one stores review-card receipts?
- What is
handbook_chunks_ftsand why does it exist?