Approval workflow database schema
An approval workflow needs six tables: people, rules, requests, steps, events and notifications. The design turns on steps: one row per approver per round, rebuilt every time a request is submitted, so the chain a request actually went through is stored and never recomputed from rules that may have changed since. Each step keeps both the person the rules picked and the person who must act now, and every action adds one row to an append-only events table.
- 6 tables, 7 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
- 10 seed rows from the blueprint
- The blueprint's test build passed 20 of 20 acceptance checks
Approval workflow 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.
- people.delegate_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.
- rules.approver_id references people.id: each rules row points to exactly one people row, and one people row can have many rules rows pointing to it.
- requests.requester_id references people.id: each requests row points to exactly one people row, and one people row can have many requests rows pointing to it.
- steps.request_id references requests.id: each steps row points to exactly one requests row, and one requests row can have many steps rows pointing to it.
- steps.assignee_id references people.id: each steps row points to exactly one people row, and one people row can have many steps rows pointing to it.
- steps.original_assignee_id references people.id: each steps row points to exactly one people row, and one people row can have many steps rows pointing to it.
- events.request_id references requests.id: each events row points to exactly one requests row, and one requests row can have many events rows pointing to it.
- events.step_id references steps.id: each events row points to at most one steps row, and one steps 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.person_id references people.id: each notifications row points to exactly one people row, and one people row can have many notifications rows pointing to it.
- notifications.request_id references requests.id: each notifications row points to exactly one requests row, and one requests row can have many notifications rows pointing to it.
Approval workflow tables
The schema has 6 tables. Every column below is read from the SQL file itself. Also described as: purchase request approval database design, approval process database tables, multi-step approval data model.
| Table | Implements | What a row is |
|---|---|---|
people | Person | One row per person who can request, approve or administer, with their manager, delegate and out-of-office date. |
rules | Rule | One row per routing rule in the approval matrix: an approver, a minimum amount and an optional category. |
requests | Request | One row per spend request, with its amount in cents, its status and a pointer to the active step. |
steps | Step | One row per approval step of one round of one request, with its assignee, original assignee and status. |
events | Event | One row per action on a request: the append-only audit log. |
notifications | Notification | One row per message to one person on one channel, written before sending so it can be retried. |
people
One row per person who can request, approve or administer.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
email | TEXT | no | Unique | ||
manager_id | TEXT | yes | References people.id | who approves this person's requests first; empty for the top of the company | |
is_admin | BOOLEAN | no | FALSE | manages people and rules; gives no approval power | |
slack_user_id | TEXT | yes | filled from the email by a Slack lookup; empty means not linked | ||
out_of_office_until | TIMESTAMP | yes | while this is in the future, new steps go to the delegate | ||
delegate_id | TEXT | yes | References people.id | who acts for this person while they are out of office | |
active | BOOLEAN | no | TRUE |
- App rule (R11): reporting lines cannot loop; reject a manager change that makes someone their own manager, directly or indirectly.
- App rule (R8): inactive people cannot be assigned a step.
rules
One row per routing rule in the approval matrix. A matching rule adds one step to the chain.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | becomes the step name, for example Finance | ||
approver_id | TEXT | no | References people.id | ||
min_amount_cents | INTEGER | no | the rule applies at or above this amount; 0 means any amountCheck: min_amount_cents >= 0 | ||
category | TEXT | yes | empty means every categoryCheck: category IN ('Hardware', 'Software', 'Services', 'Travel', 'Other') | ||
position | INTEGER | no | rules are evaluated in ascending position | ||
active | BOOLEAN | no |
- App rule (R9): changing a rule affects only future submissions, never chains already in progress.
requests
One row per spend request.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
number | INTEGER | no | Unique | human-friendly and sequential: 201, 202, ... | |
requester_id | TEXT | no | References people.id | ||
title | TEXT | no | Check: length(title) <= 120 | ||
category | TEXT | no | Check: category IN ('Hardware', 'Software', 'Services', 'Travel', 'Other') | ||
amount_cents | INTEGER | no | integer cents in the one currency the admin set (R6)Check: amount_cents > 0 | ||
vendor | TEXT | yes | |||
justification | TEXT | no | Check: length(justification) >= 10 | ||
needed_by | DATE | yes | |||
link | TEXT | yes | URL of a quote, invoice or product page; v1 takes a link instead of a file upload | ||
status | TEXT | no | 'draft' | Check: status IN ('draft', 'pending', 'changes_requested', 'approved', 'rejected', 'cancelled') | |
current_step_id | TEXT | yes | the active step in steps. No foreign key: requests and steps point at each other, and steps is created later | ||
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP | ||
submitted_at | TIMESTAMP | yes | |||
decided_at | TIMESTAMP | yes | set on final approval, rejection or cancellation |
- Index
requests_requester_idxon(requester_id). Lists and filters behind the main screens. - Index
requests_status_idxon(status) - App rule (R4): a request can be edited only in draft or changes_requested.
- App rule (R7): numbers are sequential and never reused.
- App rule (section 7): approved, rejected and cancelled are final; only comments are allowed after that.
steps
One row per approval step. The chain is rebuilt on every submission, so a resubmitted request gets new rows in a new round.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
request_id | TEXT | no | References requests.id | ||
round | INTEGER | no | 1 | starts at 1 and goes up by one each time the request is resubmittedCheck: round >= 1 | |
position | INTEGER | no | order inside the round: 1, 2, 3Check: position >= 1 | ||
name | TEXT | no | Manager, or the name of the rule that added the step | ||
assignee_id | TEXT | no | References people.id | who must act now | |
original_assignee_id | TEXT | no | References people.id | who the rules picked, before delegation, escalation or reassignment | |
status | TEXT | no | 'waiting' | Check: status IN ('waiting', 'active', 'approved', 'rejected', 'changes_requested', 'skipped', 'cancelled') | |
skip_reason | TEXT | yes | for example: Same approver as step 1 | ||
activated_at | TIMESTAMP | yes | the reminder and escalation clocks start here (R12 resets it) | ||
decided_at | TIMESTAMP | yes | |||
last_reminded_at | TIMESTAMP | yes | |||
escalated_at | TIMESTAMP | yes |
- Unique together:
request_id, round, position - Index
steps_request_idxon(request_id, round, position) - Unique index
steps_one_active_idxon(request_id)wherestatus = 'active'. The chain is sequential: at most one step per request is active at a time. - Index
steps_active_assignee_idxon(assignee_id)wherestatus = 'active'. The approvals inbox and the hourly reminder and escalation job read active steps only. - App rule (R1): only the assignee of the active step can approve, reject or send back.
- App rule (R2): the assignee is never the requester; routing reassigns such a step and the action checks it again.
- App rule (R10): nobody approves the same request twice in one round; a later step for the same person is skipped.
events
The audit log: one row per action.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
request_id | TEXT | no | References requests.id | ||
step_id | TEXT | yes | References steps.id | empty for actions on the whole request | |
actor_id | TEXT | yes | References people.id | empty for system actions such as reminders and escalation | |
type | TEXT | no | Check: type IN ('created', 'edited', 'submitted', 'step_activated', 'approved', 'rejected', 'changes_requested', 'resubmitted', 'cancelled', 'commented', 'delegated', 'reminded', 'escalated', 'reassigned', 'skipped', 'completed') | ||
comment | TEXT | yes | App rule (R3): at least 3 characters on rejected and changes_requested | ||
data | TEXT | yes | JSON as text, for example old and new amount on an edit, or from and to assignee | ||
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP |
- Index
events_request_idxon(request_id, created_at) - App rule (R5): every action writes exactly one row, and rows are never updated or deleted. Give the app no code path that does either.
notifications
The notification outbox: one row per message to one person on one channel. Written first, then sent.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
person_id | TEXT | no | References people.id | the recipient | |
channel | TEXT | no | Check: channel IN ('email', 'slack', 'in_app') | ||
kind | TEXT | no | which trigger in section 10 produced it, for example step_activated or reminder | ||
request_id | TEXT | no | References requests.id | ||
payload | TEXT | yes | JSON as text: what the message needs to render | ||
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP | ||
sent_at | TIMESTAMP | yes | empty until the send succeeds | ||
error | TEXT | yes | the last send error, if any |
- Index
notifications_unsent_idxon(created_at)wheresent_at IS NULL. The retry job reads unsent notifications only. - App rule (section 10): a failed send keeps its row and its error and is retried on the next scheduled run; it never blocks an approval.
The SQL
The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download approval-workflow.sql or copy it from here.
-- Approval Workflow database schema
-- Source: https://personalsoftwarefactory.com/blueprints/approval-workflow (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.
-- One row per person who can request, approve or administer.
-- App rule (R11): reporting lines cannot loop; reject a manager change that makes someone their own manager, directly or indirectly.
-- App rule (R8): inactive people cannot be assigned a step.
CREATE TABLE people (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
manager_id TEXT REFERENCES people(id), -- who approves this person's requests first; empty for the top of the company
is_admin BOOLEAN NOT NULL DEFAULT FALSE, -- manages people and rules; gives no approval power
slack_user_id TEXT, -- filled from the email by a Slack lookup; empty means not linked
out_of_office_until TIMESTAMP, -- while this is in the future, new steps go to the delegate
delegate_id TEXT REFERENCES people(id), -- who acts for this person while they are out of office
active BOOLEAN NOT NULL DEFAULT TRUE
);
-- One row per routing rule in the approval matrix. A matching rule adds one step to the chain.
-- App rule (R9): changing a rule affects only future submissions, never chains already in progress.
CREATE TABLE rules (
id TEXT PRIMARY KEY,
name TEXT NOT NULL, -- becomes the step name, for example Finance
approver_id TEXT NOT NULL REFERENCES people(id),
min_amount_cents INTEGER NOT NULL CHECK (min_amount_cents >= 0), -- the rule applies at or above this amount; 0 means any amount
category TEXT CHECK (category IN ('Hardware', 'Software', 'Services', 'Travel', 'Other')), -- empty means every category
position INTEGER NOT NULL, -- rules are evaluated in ascending position
active BOOLEAN NOT NULL
);
-- One row per spend request.
-- App rule (R4): a request can be edited only in draft or changes_requested.
-- App rule (R7): numbers are sequential and never reused.
-- App rule (section 7): approved, rejected and cancelled are final; only comments are allowed after that.
CREATE TABLE requests (
id TEXT PRIMARY KEY,
number INTEGER NOT NULL UNIQUE, -- human-friendly and sequential: 201, 202, ...
requester_id TEXT NOT NULL REFERENCES people(id),
title TEXT NOT NULL CHECK (length(title) <= 120),
category TEXT NOT NULL CHECK (category IN ('Hardware', 'Software', 'Services', 'Travel', 'Other')),
amount_cents INTEGER NOT NULL CHECK (amount_cents > 0), -- integer cents in the one currency the admin set (R6)
vendor TEXT,
justification TEXT NOT NULL CHECK (length(justification) >= 10),
needed_by DATE,
link TEXT, -- URL of a quote, invoice or product page; v1 takes a link instead of a file upload
status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'pending', 'changes_requested', 'approved', 'rejected', 'cancelled')),
current_step_id TEXT, -- the active step in steps. No foreign key: requests and steps point at each other, and steps is created later
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
submitted_at TIMESTAMP,
decided_at TIMESTAMP -- set on final approval, rejection or cancellation
);
-- One row per approval step. The chain is rebuilt on every submission, so a resubmitted request gets new rows in a new round.
-- App rule (R1): only the assignee of the active step can approve, reject or send back.
-- App rule (R2): the assignee is never the requester; routing reassigns such a step and the action checks it again.
-- App rule (R10): nobody approves the same request twice in one round; a later step for the same person is skipped.
CREATE TABLE steps (
id TEXT PRIMARY KEY,
request_id TEXT NOT NULL REFERENCES requests(id),
round INTEGER NOT NULL DEFAULT 1 CHECK (round >= 1), -- starts at 1 and goes up by one each time the request is resubmitted
position INTEGER NOT NULL CHECK (position >= 1), -- order inside the round: 1, 2, 3
name TEXT NOT NULL, -- Manager, or the name of the rule that added the step
assignee_id TEXT NOT NULL REFERENCES people(id), -- who must act now
original_assignee_id TEXT NOT NULL REFERENCES people(id), -- who the rules picked, before delegation, escalation or reassignment
status TEXT NOT NULL DEFAULT 'waiting' CHECK (status IN ('waiting', 'active', 'approved', 'rejected', 'changes_requested', 'skipped', 'cancelled')),
skip_reason TEXT, -- for example: Same approver as step 1
activated_at TIMESTAMP, -- the reminder and escalation clocks start here (R12 resets it)
decided_at TIMESTAMP,
last_reminded_at TIMESTAMP,
escalated_at TIMESTAMP,
UNIQUE (request_id, round, position)
);
-- The audit log: one row per action.
-- App rule (R5): every action writes exactly one row, and rows are never updated or deleted. Give the app no code path that does either.
CREATE TABLE events (
id TEXT PRIMARY KEY,
request_id TEXT NOT NULL REFERENCES requests(id),
step_id TEXT REFERENCES steps(id), -- empty for actions on the whole request
actor_id TEXT REFERENCES people(id), -- empty for system actions such as reminders and escalation
type TEXT NOT NULL CHECK (type IN ('created', 'edited', 'submitted', 'step_activated', 'approved', 'rejected', 'changes_requested', 'resubmitted', 'cancelled', 'commented', 'delegated', 'reminded', 'escalated', 'reassigned', 'skipped', 'completed')),
comment TEXT, -- App rule (R3): at least 3 characters on rejected and changes_requested
data TEXT, -- JSON as text, for example old and new amount on an edit, or from and to assignee
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- The notification outbox: one row per message to one person on one channel. Written first, then sent.
-- App rule (section 10): a failed send keeps its row and its error and is retried on the next scheduled run; it never blocks an approval.
CREATE TABLE notifications (
id TEXT PRIMARY KEY,
person_id TEXT NOT NULL REFERENCES people(id), -- the recipient
channel TEXT NOT NULL CHECK (channel IN ('email', 'slack', 'in_app')),
kind TEXT NOT NULL, -- which trigger in section 10 produced it, for example step_activated or reminder
request_id TEXT NOT NULL REFERENCES requests(id),
payload TEXT, -- JSON as text: what the message needs to render
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
sent_at TIMESTAMP, -- empty until the send succeeds
error TEXT -- the last send error, if any
);
-- Lists and filters behind the main screens.
CREATE INDEX requests_requester_idx ON requests (requester_id);
CREATE INDEX requests_status_idx ON requests (status);
CREATE INDEX steps_request_idx ON steps (request_id, round, position);
CREATE INDEX events_request_idx ON events (request_id, created_at);
-- The chain is sequential: at most one step per request is active at a time.
CREATE UNIQUE INDEX steps_one_active_idx ON steps (request_id) WHERE status = 'active';
-- The approvals inbox and the hourly reminder and escalation job read active steps only.
CREATE INDEX steps_active_assignee_idx ON steps (assignee_id) WHERE status = 'active';
-- The retry job reads unsent notifications only.
CREATE INDEX notifications_unsent_idx ON notifications (created_at) WHERE sent_at IS NULL;Seed data
The seed file loads the 7 people and 3 routing rules from section 14 of the blueprint, with the manager chain, admin flags and rule minimums exactly as the spec's tables give them. The spec gives no email addresses or ids, so the file uses the example.com addresses and short ids from the site's reference build. Job titles in the spec's table (Head of Ops, Finance lead) have no column and are not loaded. The spec also asks for one example request in each status but does not define those requests, so requests, steps, events and notifications start empty.
Download approval-workflow-seed.sql (10 rows). It loads with foreign keys enforced on both engines.
Load it
SQLite, into a new file:
curl -s https://personalsoftwarefactory.com/schemas/approval-workflow.sql https://personalsoftwarefactory.com/schemas/approval-workflow-seed.sql | sqlite3 approval_workflow.dbPostgreSQL, into a new database:
createdb approval_workflow && curl -s https://personalsoftwarefactory.com/schemas/approval-workflow.sql https://personalsoftwarefactory.com/schemas/approval-workflow-seed.sql | psql -v ON_ERROR_STOP=1 approval_workflowSQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.
Design decisions
The chain is stored as rows, one per step per round
Section 6 builds the chain when a request is submitted, and section 7 rebuilds it from scratch on every resubmission. Storing each step with a round number keeps the earlier rounds for the audit trail, and rule R9 holds for free: changing a rule cannot alter a chain that already exists. A schema that computes the chain from the rules at read time shows a different history after every rule change.
Each step has an assignee and an original assignee
From the build test · field report
Section 5 keeps who the rules picked (original_assignee_id) apart from who must act now (assignee_id), because delegation, escalation and reassignment in section 12 all move a step to someone else. Both columns are needed to show "delegated from Maya" in the chain. The build test found the spec did not say what to display when a delegated step later escalates (gap 9); section 9 now shows both hops, and the events table carries the from and to of each one.
A partial unique index allows one active step per request
Section 3 makes the chain sequential with one approver per step, so two active steps on one request is always a bug. The unique index on steps (request_id) where status is active makes the database refuse it. This supports the edge case in section 13, where two approvers click at the same time on stale screens and the second must fail.
requests.current_step_id has no foreign key
A request points at its active step and every step points at its request, which is a cycle. PostgreSQL will not create a reference to a table that does not exist yet, so the schema keeps the foreign key on steps.request_id and leaves current_step_id as a plain column. The app sets it in the same transaction that activates the step.
Events are insert-only, and the database is not what enforces it
Rule R5 says every action writes exactly one event and events are never edited or deleted. Portable SQL has no way to forbid updates without triggers, which this schema avoids, so the rule is a comment on the table and a property of the app: give it no code path that updates or deletes an event. The data column holds JSON as text (old and new amount, from and to assignee) because a JSON column type is not portable between the two engines.
Amounts are integer cents with a check above zero
Section 3 and rule R6 store amounts as integer cents in one currency, so there is no currency column and no decimal type. The routing comparison in section 6 (amount_cents >= min_amount_cents) is then exact integer arithmetic. A rule's minimum of 0 means any amount, and a rule with an empty category applies to every category.
Rules the database cannot check stay in the app
From the build test · field report
A requester never approves their own request (R2), nobody approves twice in one round (R10) and reporting lines cannot loop (R11). Each of these compares rows with other rows, so none fits in a CHECK constraint. The build test found that the admin screen allowed reporting loops, which breaks routing (gap 3, fixed by R11); the schema names these rules in comments on the tables so whoever builds the app sees them.
Example queries
The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.
Admin, rules: the approval matrix in evaluation order
SELECT r.position, r.name, p.name AS approver, r.min_amount_cents, COALESCE(r.category, 'any') AS category
FROM rules r
JOIN people p ON p.id = r.approver_id
WHERE r.active = TRUE
ORDER BY r.position;On the seed data: 3 rows, in order IT (Ben Cho, 0, Software), Finance (Ana Lima, 100000, any), Executive (Dana Ruiz, 1000000, any).
Which rule steps a $2,400 Hardware request gets
SELECT r.position, r.name AS step_name, p.name AS approver
FROM rules r
JOIN people p ON p.id = r.approver_id
WHERE r.active = TRUE
AND 240000 >= r.min_amount_cents
AND (r.category IS NULL OR r.category = 'Hardware')
ORDER BY r.position;On the seed data: 1 row (Finance, Ana Lima). With the Manager step first, that is the chain Maya then Ana from the spec's worked example for Jo.
Which rule steps a $12,000 Software request gets
SELECT r.position, r.name AS step_name, p.name AS approver
FROM rules r
JOIN people p ON p.id = r.approver_id
WHERE r.active = TRUE
AND 1200000 >= r.min_amount_cents
AND (r.category IS NULL OR r.category = 'Software')
ORDER BY r.position;On the seed data: 3 rows (IT: Ben Cho, Finance: Ana Lima, Executive: Dana Ruiz), the chain after the Manager step in the spec's worked example for Lee.
Admin, people: everyone with their manager
SELECT p.name, p.email, COALESCE(m.name, 'none') AS manager, CASE WHEN p.is_admin = TRUE THEN 'yes' ELSE 'no' END AS admin
FROM people p
LEFT JOIN people m ON m.id = p.manager_id
WHERE p.active = TRUE
ORDER BY p.name;On the seed data: 7 rows. Dana Ruiz has no manager and is an admin; Jo Park reports to Maya Chen.
The fallback approver when a requester has no manager
SELECT p.id, p.name
FROM people p
WHERE p.is_admin = TRUE AND p.active = TRUE AND p.id <> 'p_dana'
ORDER BY p.name
LIMIT 1;On the seed data: 1 row (Ana Lima), the Manager step for a request from Dana Ruiz, as in the spec's worked example.
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 Approval Workflow blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 20 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 20 of 20 acceptance checks in a browser.
- Read the Approval Workflow 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.