Expense management system database schema
Eight tables. reports carries the workflow from draft through submitted and approved to reimbursed, with the approver fixed at submission and the payment reference set only when paid. expenses hold integer cents, and a mileage expense stores its miles in tenths and the rate copied when it was created, with a CHECK that the amount matches. Flags such as over limit and missing receipt are computed from current data, never stored.
- 8 tables, 4 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
- 27 seed rows from the blueprint
- The blueprint's test build passed 24 of 24 acceptance checks
Expense reports entity-relationship diagram
Relationships
- people.manager_id references people.id: each people row points to at most one another people row, and one people row can have many people rows pointing to it.
- reports.owner_id references people.id: each reports row points to exactly one people row, and one people row can have many reports rows pointing to it.
- reports.approver_id references people.id: each reports row points to at most one people row, and one people row can have many reports rows pointing to it.
- expenses.report_id references reports.id: each expenses row points to exactly one reports row, and one reports row can have many expenses rows pointing to it.
- expenses.category_id references categories.id: each expenses row points to exactly one categories row, and one categories row can have many expenses rows pointing to it.
- comments.report_id references reports.id: each comments row points to exactly one reports row, and one reports row can have many comments rows pointing to it.
- comments.author_id references people.id: each comments row points to exactly one people row, and one people row can have many comments rows pointing to it.
- events.report_id references reports.id: each events row points to exactly one reports row, and one reports row can have many events rows pointing to it.
- events.actor_id references people.id: each events row points to at most one people row, and one people row can have many events rows pointing to it.
- notifications.report_id references reports.id: each notifications row points to exactly one reports row, and one reports row can have many notifications rows pointing to it.
Expense reports tables
The schema has 8 tables. Every column below is read from the SQL file itself. Also described as: expense reimbursement database design, expense report database tables, employee expense tracking data model.
| Table | Implements | What a row is |
|---|---|---|
people | Person | A person who files reports, with their manager and finance flag. |
settings | Settings | The one row with the receipt threshold, mileage rate, maximum age and reminder delay. |
categories | Category | An expense category with an optional limit and whether it needs receipts. |
reports | Report | A numbered report that moves from draft to reimbursed, with its approver and payment details. |
expenses | Expense | One expense in cents on a report, with its receipt, explanation and mileage fields. |
comments | Comment | A comment on a report, including rejection and reassignment comments. |
events | Event | One append-only entry in a report's activity log. |
notifications | Notification | One email in the outbox with its status. |
people
A person who files reports. manager_id is the usual approver (R7).
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
email | TEXT | no | Unique | stored lowercaseCheck: email = lower(email) | |
manager_id | TEXT | yes | References people.id | never the person themselvesCheck: manager_id <> id | |
is_finance | BOOLEAN | no | FALSE | ||
active | BOOLEAN | no | TRUE |
- App rule (R10): no manager loops; a person who is the approver of a submitted report cannot be deactivated until it is reassigned.
settings
The single settings row. id is fixed at 1 so the table can never hold a second row.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | INTEGER | no | Primary key | Check: id = 1 | |
company_name | TEXT | no | |||
time_zone | TEXT | no | 'America/New_York' | IANA name; decides what "today" is | |
receipt_required_cents | INTEGER | no | 7500 | receipts required at or above this amountCheck: receipt_required_cents >= 0 | |
mileage_rate_cents_per_mile | INTEGER | no | 70 | set to the rate your company paysCheck: mileage_rate_cents_per_mile >= 0 | |
max_age_days | INTEGER | no | 90 | older expenses are flaggedCheck: max_age_days >= 1 | |
reminder_days | INTEGER | no | 3 | remind the approver after this many whole daysCheck: reminder_days >= 1 |
categories
An expense category with an optional limit. Exactly one category is the mileage category.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | Unique | ||
limit_cents | INTEGER | yes | NULL means no limit; above it the expense is flagged Over limitCheck: limit_cents >= 0 | ||
requires_receipt | BOOLEAN | no | TRUE | ||
is_mileage | BOOLEAN | no | FALSE | ||
active | BOOLEAN | no | TRUE |
- Unique index
categories_one_mileageon(is_mileage)whereis_mileage = TRUE
reports
An expense report: a batch of expenses that is submitted, approved and reimbursed together.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
number | INTEGER | no | Unique | Check: number >= 101 | |
title | TEXT | no | Check: length(title) BETWEEN 1 AND 100 | ||
owner_id | TEXT | no | References people.id | ||
status | TEXT | no | 'draft' | Check: status IN ('draft', 'submitted', 'approved', 'reimbursed') | |
approver_id | TEXT | yes | References people.id | ||
submitted_at | TIMESTAMP | yes | |||
approved_at | TIMESTAMP | yes | |||
reimbursed_at | TIMESTAMP | yes | |||
payment_reference | TEXT | yes | set when reimbursedCheck: length(payment_reference) BETWEEN 1 AND 100 | ||
paid_on | DATE | yes | set when reimbursed; today or earlier | ||
version | INTEGER | no | 1 | goes up on every change (R9) |
- Table check:
approver_id <> owner_id - Table check:
status = 'draft' OR (approver_id IS NOT NULL AND submitted_at IS NOT NULL) - Table check:
status NOT IN ('approved', 'reimbursed') OR approved_at IS NOT NULL - Table check:
(status = 'reimbursed' AND reimbursed_at IS NOT NULL AND payment_reference IS NOT NULL AND paid_on IS NOT NULL) OR (status <> 'reimbursed' AND reimbursed_at IS NULL AND payment_reference IS NULL AND paid_on IS NULL) - App rule: number is sequential from 101 (shown as R-101). Only draft reports can be edited; reimbursed is final.
- App rule (R7): approver_id is set at submission: the active manager of the owner, else the first active finance person by name.
expenses
One expense on a report. Flags (over limit, missing receipt, old, possible duplicate) are computed, never stored.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
report_id | TEXT | no | References reports.id | ||
category_id | TEXT | no | References categories.id | ||
date | DATE | no | |||
merchant | TEXT | no | for mileage, the tripCheck: length(merchant) BETWEEN 1 AND 100 | ||
amount_cents | INTEGER | no | $0.01 to $10,000.00Check: amount_cents BETWEEN 1 AND 1000000 | ||
miles_tenths | INTEGER | yes | mileage only; 0.1 to 2,000 milesCheck: miles_tenths BETWEEN 1 AND 20000 | ||
rate_cents_per_mile | INTEGER | yes | mileage only; copied from settings when created, never changedCheck: rate_cents_per_mile >= 0 | ||
description | TEXT | yes | Check: length(description) <= 300 | ||
explanation | TEXT | yes | required before submitting when the expense has a flag (R4)Check: length(explanation) BETWEEN 5 AND 300 | ||
receipt_file | TEXT | yes | reference to a private file (JPG, PNG or PDF up to 10 MB), never a public URL | ||
created_at | TIMESTAMP | no |
- Table check:
(miles_tenths IS NULL AND rate_cents_per_mile IS NULL) OR (miles_tenths IS NOT NULL AND rate_cents_per_mile IS NOT NULL) - Table check:
miles_tenths IS NULL OR amount_cents = (2 * miles_tenths * rate_cents_per_mile + 10) / 20 - Index
expenses_reporton(report_id) - Index
expenses_date_amounton(date, amount_cents) - App rule (R2): date not in the future and the category active when saved. Mileage fields only on the mileage category.
comments
A comment on a report, including rejection and reassignment comments.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
report_id | TEXT | no | References reports.id | ||
author_id | TEXT | no | References people.id | ||
text | TEXT | no | Check: length(text) BETWEEN 1 AND 1000 | ||
created_at | TIMESTAMP | no |
events
One action on a report. Append-only (R11).
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
report_id | TEXT | no | References reports.id | ||
actor_id | TEXT | yes | References people.id | NULL for the system | |
type | TEXT | no | Check: type IN ('created', 'expense_added', 'expense_edited', 'expense_removed', 'submitted', 'retracted', 'approved', 'rejected', 'reimbursed', 'reminded', 'commented', 'reassigned') | ||
data | TEXT | yes | JSON stored as text | ||
created_at | TIMESTAMP | no |
notifications
One email in the outbox. Written first, then sent; a failure never blocks the action.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
kind | TEXT | no | |||
to_email | TEXT | no | |||
report_id | TEXT | no | References reports.id | ||
subject | TEXT | no | |||
body | TEXT | no | |||
status | TEXT | no | 'pending' | Check: status IN ('pending', 'sent', 'logged', 'failed') | |
error | TEXT | yes | |||
created_at | TIMESTAMP | no | |||
sent_at | TIMESTAMP | yes |
- Index
notifications_statuson(status)
The SQL
The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download expense-reports.sql or copy it from here.
-- Expense Reports database schema
-- Source: https://personalsoftwarefactory.com/blueprints/expense-reports (data model, section 5 of the blueprint)
-- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC.
-- License: MIT. Free to copy and adapt.
-- A person who files reports. manager_id is the usual approver (R7).
-- App rule (R10): no manager loops; a person who is the approver of a submitted report cannot be deactivated until it is reassigned.
CREATE TABLE people (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase
manager_id TEXT REFERENCES people(id) CHECK (manager_id <> id), -- never the person themselves
is_finance BOOLEAN NOT NULL DEFAULT FALSE,
active BOOLEAN NOT NULL DEFAULT TRUE
);
-- The single settings row. id is fixed at 1 so the table can never hold a second row.
CREATE TABLE settings (
id INTEGER PRIMARY KEY CHECK (id = 1),
company_name TEXT NOT NULL,
time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; decides what "today" is
receipt_required_cents INTEGER NOT NULL DEFAULT 7500 CHECK (receipt_required_cents >= 0), -- receipts required at or above this amount
mileage_rate_cents_per_mile INTEGER NOT NULL DEFAULT 70 CHECK (mileage_rate_cents_per_mile >= 0), -- set to the rate your company pays
max_age_days INTEGER NOT NULL DEFAULT 90 CHECK (max_age_days >= 1), -- older expenses are flagged
reminder_days INTEGER NOT NULL DEFAULT 3 CHECK (reminder_days >= 1) -- remind the approver after this many whole days
);
-- An expense category with an optional limit. Exactly one category is the mileage category.
CREATE TABLE categories (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
limit_cents INTEGER CHECK (limit_cents >= 0), -- NULL means no limit; above it the expense is flagged Over limit
requires_receipt BOOLEAN NOT NULL DEFAULT TRUE,
is_mileage BOOLEAN NOT NULL DEFAULT FALSE,
active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE UNIQUE INDEX categories_one_mileage ON categories (is_mileage) WHERE is_mileage = TRUE; -- at most one mileage category
-- An expense report: a batch of expenses that is submitted, approved and reimbursed together.
-- App rule: number is sequential from 101 (shown as R-101). Only draft reports can be edited; reimbursed is final.
-- App rule (R7): approver_id is set at submission: the active manager of the owner, else the first active finance person by name.
CREATE TABLE reports (
id TEXT PRIMARY KEY,
number INTEGER NOT NULL UNIQUE CHECK (number >= 101),
title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 100),
owner_id TEXT NOT NULL REFERENCES people(id),
status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'submitted', 'approved', 'reimbursed')),
approver_id TEXT REFERENCES people(id),
submitted_at TIMESTAMP,
approved_at TIMESTAMP,
reimbursed_at TIMESTAMP,
payment_reference TEXT CHECK (length(payment_reference) BETWEEN 1 AND 100), -- set when reimbursed
paid_on DATE, -- set when reimbursed; today or earlier
version INTEGER NOT NULL DEFAULT 1, -- goes up on every change (R9)
CHECK (approver_id <> owner_id), -- nobody approves their own report
CHECK (status = 'draft' OR (approver_id IS NOT NULL AND submitted_at IS NOT NULL)),
CHECK (status NOT IN ('approved', 'reimbursed') OR approved_at IS NOT NULL),
CHECK ((status = 'reimbursed' AND reimbursed_at IS NOT NULL AND payment_reference IS NOT NULL AND paid_on IS NOT NULL)
OR (status <> 'reimbursed' AND reimbursed_at IS NULL AND payment_reference IS NULL AND paid_on IS NULL))
);
-- One expense on a report. Flags (over limit, missing receipt, old, possible duplicate) are computed, never stored.
-- App rule (R2): date not in the future and the category active when saved. Mileage fields only on the mileage category.
CREATE TABLE expenses (
id TEXT PRIMARY KEY,
report_id TEXT NOT NULL REFERENCES reports(id),
category_id TEXT NOT NULL REFERENCES categories(id),
date DATE NOT NULL,
merchant TEXT NOT NULL CHECK (length(merchant) BETWEEN 1 AND 100), -- for mileage, the trip
amount_cents INTEGER NOT NULL CHECK (amount_cents BETWEEN 1 AND 1000000), -- $0.01 to $10,000.00
miles_tenths INTEGER CHECK (miles_tenths BETWEEN 1 AND 20000), -- mileage only; 0.1 to 2,000 miles
rate_cents_per_mile INTEGER CHECK (rate_cents_per_mile >= 0), -- mileage only; copied from settings when created, never changed
description TEXT CHECK (length(description) <= 300),
explanation TEXT CHECK (length(explanation) BETWEEN 5 AND 300), -- required before submitting when the expense has a flag (R4)
receipt_file TEXT, -- reference to a private file (JPG, PNG or PDF up to 10 MB), never a public URL
created_at TIMESTAMP NOT NULL,
CHECK ((miles_tenths IS NULL AND rate_cents_per_mile IS NULL) OR (miles_tenths IS NOT NULL AND rate_cents_per_mile IS NOT NULL)),
CHECK (miles_tenths IS NULL OR amount_cents = (2 * miles_tenths * rate_cents_per_mile + 10) / 20) -- R5: tenths x rate / 10, half up
);
CREATE INDEX expenses_report ON expenses (report_id);
CREATE INDEX expenses_date_amount ON expenses (date, amount_cents); -- for the possible-duplicate check
-- A comment on a report, including rejection and reassignment comments.
CREATE TABLE comments (
id TEXT PRIMARY KEY,
report_id TEXT NOT NULL REFERENCES reports(id),
author_id TEXT NOT NULL REFERENCES people(id),
text TEXT NOT NULL CHECK (length(text) BETWEEN 1 AND 1000),
created_at TIMESTAMP NOT NULL
);
-- One action on a report. Append-only (R11).
CREATE TABLE events (
id TEXT PRIMARY KEY,
report_id TEXT NOT NULL REFERENCES reports(id),
actor_id TEXT REFERENCES people(id), -- NULL for the system
type TEXT NOT NULL CHECK (type IN ('created', 'expense_added', 'expense_edited', 'expense_removed', 'submitted', 'retracted', 'approved', 'rejected', 'reimbursed', 'reminded', 'commented', 'reassigned')),
data TEXT, -- JSON stored as text
created_at TIMESTAMP NOT NULL
);
-- One email in the outbox. Written first, then sent; a failure never blocks the action.
CREATE TABLE notifications (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
to_email TEXT NOT NULL,
report_id TEXT NOT NULL REFERENCES reports(id),
subject TEXT NOT NULL,
body TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'logged', 'failed')),
error TEXT,
created_at TIMESTAMP NOT NULL,
sent_at TIMESTAMP
);
CREATE INDEX notifications_status ON notifications (status);Seed data
Loads section 14 with its example dates (seed day Tue 13 Oct 2026): settings, 6 people, 7 categories, 4 reports and 9 expenses, with placeholder receipt references as the spec asks. The spec gives no times, expense created_at values or report versions, so timestamps are 12:00 New York time on the date given, created_at is the expense date and every report is at version 1. No events, comments or emails are loaded: the spec lists none.
Download expense-reports-seed.sql (27 rows). It loads with foreign keys enforced on both engines.
Load it
SQLite, into a new file:
curl -s https://personalsoftwarefactory.com/schemas/expense-reports.sql https://personalsoftwarefactory.com/schemas/expense-reports-seed.sql | sqlite3 expense_reports.dbPostgreSQL, into a new database:
createdb expense_reports && curl -s https://personalsoftwarefactory.com/schemas/expense-reports.sql https://personalsoftwarefactory.com/schemas/expense-reports-seed.sql | psql -v ON_ERROR_STOP=1 expense_reportsSQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.
Design decisions
Mileage keeps its own rate and checks its own amount
R5 copies the mileage rate onto the expense when it is created, so changing the company rate never changes old expenses. The expense stores miles_tenths and rate_cents_per_mile, and a CHECK requires amount_cents to equal tenths times rate divided by 10, rounded half up, in integers. 38.5 miles at $0.70 must be 2,695 cents and nothing else.
Flags are computed, not stored
From the build test · field report
There are no flag columns. The build test found it unclear when flags are computed; section 6 now says they come from current data whenever an expense or report is shown, and Old expense is judged against the submission date once a report is submitted. A stored flag would go stale when a receipt is attached or a limit changes, so the flag query below computes all four.
Report status and its dates must agree
CHECKs require an approver and submitted_at on every report past draft, approved_at on approved and reimbursed reports, and payment_reference, paid_on and reimbursed_at exactly when the status is reimbursed (section 8). A report can never be marked paid without a reference, and a draft can never carry one.
Nobody approves their own report
From the build test · field report
approver_id is fixed at submission by R7 and a CHECK keeps it different from owner_id. The build test found that changing a report's approver had no event, email or comment rule; section 8 now has a Reassign action with a required comment and a reassigned event, which is why comments and the reassigned event type exist.
At most one mileage category
Category.is_mileage marks the one category whose amount is computed from miles (section 5 says exactly one, "Mileage"). A partial unique index on is_mileage WHERE is_mileage = TRUE stops a second one; that there is at least one is left to the seed and the app.
Receipts are private file references
From the build test · field report
receipt_file holds a reference to a file in private storage, never a public URL, because R3 serves receipts only through an authenticated route to people who can see the report. The build test found R3 and R8 named different people who can see receipts; both now use R8's rule, which the app applies on every download.
Example queries
The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.
Reports with totals and status
SELECT 'R-' || r.number AS report, r.title, o.name AS owner, r.status,
COALESCE(a.name, '') AS approver,
(SELECT SUM(e.amount_cents) FROM expenses e WHERE e.report_id = r.id) AS total_cents,
COALESCE(r.payment_reference, '') AS payment_reference
FROM reports r
JOIN people o ON o.id = r.owner_id
LEFT JOIN people a ON a.id = r.approver_id
ORDER BY r.number;On the seed data: 4 rows; R-101 Boston client visit by Lee Park is a draft totalling 47,455 cents ($474.55).
Flags on a report's expenses
SELECT e.merchant, c.name AS category, e.amount_cents,
CASE WHEN c.limit_cents IS NOT NULL AND e.amount_cents > c.limit_cents THEN 1 ELSE 0 END AS over_limit,
CASE WHEN c.requires_receipt = TRUE AND e.amount_cents >= s.receipt_required_cents AND e.receipt_file IS NULL THEN 1 ELSE 0 END AS missing_receipt,
CASE WHEN e.date < '2026-07-15' THEN 1 ELSE 0 END AS old_expense,
CASE WHEN EXISTS (
SELECT 1 FROM expenses d JOIN reports dr ON dr.id = d.report_id
WHERE d.id <> e.id AND dr.owner_id = r.owner_id
AND d.date = e.date AND d.amount_cents = e.amount_cents
AND lower(replace(d.merchant, ' ', '')) = lower(replace(e.merchant, ' ', ''))
AND NOT (dr.status = 'reimbursed' AND dr.reimbursed_at < '2026-07-15')
) THEN 1 ELSE 0 END AS possible_duplicate,
COALESCE(e.explanation, '') AS explanation
FROM expenses e
JOIN reports r ON r.id = e.report_id
JOIN categories c ON c.id = e.category_id
JOIN settings s ON s.id = 1
WHERE r.number = 101
ORDER BY e.date, e.merchant;On the seed data, for R-101 with today as 2026-10-13: 4 rows; Dinner with Acme is over limit and Hotel, 1 night is missing a receipt.
Reports waiting for an approver
SELECT 'R-' || r.number AS report, r.title, o.name AS owner,
SUM(e.amount_cents) AS total_cents, COUNT(e.id) AS expenses
FROM reports r
JOIN people o ON o.id = r.owner_id
JOIN people a ON a.id = r.approver_id
JOIN expenses e ON e.report_id = r.id
WHERE r.status = 'submitted' AND a.email = 'maya@fernway.com'
GROUP BY r.number, r.title, o.name, r.submitted_at
ORDER BY r.submitted_at;On the seed data, for Maya Chen: 1 row; R-102 October software from Jo Park, 20,400 cents in 2 expenses.
Finance payment queue by employee
SELECT o.name AS employee, COUNT(DISTINCT r.id) AS reports, SUM(e.amount_cents) AS total_cents
FROM reports r
JOIN people o ON o.id = r.owner_id
JOIN expenses e ON e.report_id = r.id
WHERE r.status = 'approved'
GROUP BY o.name
ORDER BY o.name;On the seed data: 1 row; Sam Okafor, 1 report, 13,280 cents ($132.80).
Approved and reimbursed spending by category
SELECT c.name AS category, COUNT(*) AS expenses, SUM(e.amount_cents) AS total_cents
FROM expenses e
JOIN reports r ON r.id = e.report_id
JOIN categories c ON c.id = e.category_id
WHERE r.status IN ('approved', 'reimbursed')
AND e.date >= '2026-05-01' AND e.date <= '2026-10-31'
GROUP BY c.name
ORDER BY total_cents DESC;On the seed data, May to October 2026: 3 rows; Other is largest at 89,000 cents ($890.00).
What was not tested
- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.
The blueprint this schema comes from
Tables are the easy half. This schema is section 5 of the Expense Reports blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 24 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 24 of 24 acceptance checks in a browser.
- Read the Expense Reports blueprint: rules, screens, build prompts and acceptance checks
- Read the build-test field report: what the test build got wrong and what was not tested
- All database schemas
- How we test blueprints
Follow new blueprints
Every new blueprint and build test appears in the feed and in the public GitHub repository. No account, no email list.