Maintenance work order system database schema
Seventeen tables. work_orders carries the design: a status with CHECKs for the hold reason and completion time, a unique link back to the request it came from, and a partial unique index that lets a preventive schedule have only one work order that is not done. Requests and work orders reference each other, so the foreign key sits on work_orders.request_id and requests.work_order_id is a plain column that also records duplicates. Checklists are copied onto each work order as rows, so a reading, its flag and a not-applicable reason belong to that job and not to the template.
- 17 tables, 8 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
- 45 seed rows from the blueprint
- The blueprint's test build passed 17 of 17 acceptance checks
Maintenance requests entity-relationship diagram
Relationships
- assets.location_id references locations.id: each assets row points to exactly one locations row, and one locations row can have many assets rows pointing to it.
- checklist_template_items.template_id references checklist_templates.id: each checklist_template_items row points to exactly one checklist_templates row, and one checklist_templates row can have many checklist_template_items rows pointing to it. On delete: cascade.
- schedules.asset_id references assets.id: each schedules row points to at most one assets row, and one assets row can have many schedules rows pointing to it.
- schedules.location_id references locations.id: each schedules row points to exactly one locations row, and one locations row can have many schedules rows pointing to it.
- schedules.checklist_template_id references checklist_templates.id: each schedules row points to at most one checklist_templates row, and one checklist_templates row can have many schedules rows pointing to it.
- schedules.assignee_id references people.id: each schedules row points to at most one people row, and one people row can have many schedules rows pointing to it.
- requests.location_id references locations.id: each requests row points to exactly one locations row, and one locations row can have many requests rows pointing to it.
- requests.asset_id references assets.id: each requests row points to at most one assets row, and one assets row can have many requests rows pointing to it.
- request_photos.request_id references requests.id: each request_photos row points to exactly one requests row, and one requests row can have many request_photos rows pointing to it. On delete: cascade.
- work_orders.location_id references locations.id: each work_orders row points to exactly one locations row, and one locations row can have many work_orders rows pointing to it.
- work_orders.asset_id references assets.id: each work_orders row points to at most one assets row, and one assets row can have many work_orders rows pointing to it.
- work_orders.assignee_id references people.id: each work_orders row points to at most one people row, and one people row can have many work_orders rows pointing to it.
- work_orders.request_id references requests.id: each work_orders row points to at most one requests row, and one requests row can have at most one work_orders row pointing to it.
- work_orders.schedule_id references schedules.id: each work_orders row points to at most one schedules row, and one schedules row can have many work_orders rows pointing to it.
- work_orders.created_by references people.id: each work_orders row points to at most one people row, and one people row can have many work_orders rows pointing to it.
- checklist_items.work_order_id references work_orders.id: each checklist_items row points to exactly one work_orders row, and one work_orders row can have many checklist_items rows pointing to it. On delete: cascade.
- time_entries.work_order_id references work_orders.id: each time_entries row points to exactly one work_orders row, and one work_orders row can have many time_entries rows pointing to it. On delete: cascade.
- time_entries.person_id references people.id: each time_entries row points to exactly one people row, and one people row can have many time_entries rows pointing to it.
- parts_used.work_order_id references work_orders.id: each parts_used row points to exactly one work_orders row, and one work_orders row can have many parts_used rows pointing to it. On delete: cascade.
- comments.work_order_id references work_orders.id: each comments row points to exactly one work_orders row, and one work_orders row can have many comments rows pointing to it. On delete: cascade.
- 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.
- comment_photos.comment_id references comments.id: each comment_photos row points to exactly one comments row, and one comments row can have many comment_photos rows pointing to it. On delete: cascade.
Maintenance requests tables
The schema has 17 tables. Every column below is read from the SQL file itself. Also described as: CMMS database design, work order management database tables, facility maintenance request data model.
| Table | Implements | What a row is |
|---|---|---|
settings | Settings | The one row of organization settings: name, time zone, due days per priority and the preventive lead time. |
people | Person | An admin or technician with an account; requesters are not people. |
locations | Location | A building or site whose id is used in the public request form address. |
assets | Asset | A piece of equipment at a location, with a unique QR code token. |
checklist_templates | ChecklistTemplate | A named checklist that schedules and work orders copy items from. |
checklist_template_items | child table of ChecklistTemplate | One item of a template, in order, with its kind and, for number items, unit and range. |
schedules | Schedule | A preventive maintenance schedule with its interval, mode and next due date. |
requests | Request | A problem reported through the public form, with its requester and triage outcome. |
request_photos | child table of Request | One photo on a request, stored under a random file name, at most five per request. |
work_orders | WorkOrder | A job to do, with priority, assignee, due date and status. |
checklist_items | ChecklistItem | One checklist item on one work order, with its reading, done mark, flag and not-applicable reason. |
time_entries | TimeEntry | Whole minutes one person spent on one work order. |
parts_used | PartUsed | A part used on a work order, with quantity and unit cost in cents. |
comments | Comment | A comment on a work order, public only when the author chose to show it to the requester. |
comment_photos | child table of Comment | One photo on a comment, stored under a random file name, at most five per comment. |
events | Event | An append-only record of one change. |
notifications | Notification | One email in the outbox, with its delivery status. |
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 | |
org_name | TEXT | no | |||
time_zone | TEXT | no | 'America/Chicago' | IANA name; today, due dates and reports use this zone | |
due_days_high | INTEGER | no | 0 | days from today to due_on for a high priority work order (0 = due today)Check: due_days_high >= 0 | |
due_days_medium | INTEGER | no | 3 | Check: due_days_medium >= 0 | |
due_days_low | INTEGER | no | 7 | Check: due_days_low >= 0 | |
pm_lead_days | INTEGER | no | 7 | a schedule creates its work order this many days before next_due_onCheck: pm_lead_days >= 0 |
people
A staff member with an account. Requesters have no account and are not people.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
email | TEXT | no | Unique | stored lowercaseCheck: email = lower(email) | |
role | TEXT | no | Check: role IN ('admin', 'technician') | ||
active | BOOLEAN | no | TRUE |
- App rule (section 13): deactivating a technician unassigns their open work orders and their schedules.
locations
A building or site, such as one restaurant. The public request form is /r/<id>.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
address | TEXT | yes |
assets
A piece of equipment at a location, with a QR code that opens the request form for it.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
location_id | TEXT | no | References locations.id | ||
name | TEXT | no | |||
category | TEXT | yes | |||
make_model | TEXT | yes | free text, may be blank | ||
serial_number | TEXT | yes | the spec field serial; renamed because SERIAL is a PostgreSQL type keyword. Free text, may be blank | ||
qr_code | TEXT | no | Unique | random token; the QR label opens /a/<qr_code> | |
active | BOOLEAN | no | TRUE |
- Index
assets_locationon(location_id) - App rule (section 13): deactivating an asset pauses its schedules.
checklist_templates
A named checklist that schedules and work orders copy their items from.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no |
checklist_template_items
One item of a checklist template, in order. Stores the template items list.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
template_id | TEXT | no | Primary key (part); References checklist_templates.id, on delete cascade | ||
position | INTEGER | no | Primary key (part) | Check: position >= 1 | |
label | TEXT | no | |||
kind | TEXT | no | Check: kind IN ('check', 'number', 'pass_fail') | ||
unit | TEXT | yes | number items only, e.g. degrees F | ||
min | NUMERIC | yes | number items only: a reading below this is flagged | ||
max | NUMERIC | yes | number items only: a reading above this is flagged |
- Table check:
kind = 'number' OR (unit IS NULL AND min IS NULL AND max IS NULL) - Table check:
min IS NULL OR max IS NULL OR min <= max
schedules
A preventive maintenance schedule that creates work orders ahead of each due date.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
title | TEXT | no | |||
asset_id | TEXT | yes | References assets.id | ||
location_id | TEXT | no | References locations.id | ||
checklist_template_id | TEXT | yes | References checklist_templates.id | optional; copied onto each work order | |
every_n | INTEGER | no | Check: every_n >= 1 | ||
every_unit | TEXT | no | Check: every_unit IN ('days', 'weeks', 'months') | ||
mode | TEXT | no | Check: mode IN ('fixed', 'after_completion') | ||
next_due_on | DATE | no | |||
assignee_id | TEXT | yes | References people.id | NULL when unassigned | |
priority | TEXT | no | Check: priority IN ('high', 'medium', 'low') | ||
active | BOOLEAN | no | TRUE | FALSE when paused |
- App rule (section 7): the daily job creates the next work order pm_lead_days before next_due_on; next_due_on moves only when the work order due on it is completed.
- App rule (section 7): fixed schedules advance from the previous due date, after_completion schedules from the completion date; months are calendar months.
requests
A problem reported through the public form, waiting for triage or already handled.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
number | TEXT | no | Unique | e.g. REQ-0032Check: substr(number, 1, 4) = 'REQ-' | |
location_id | TEXT | no | References locations.id | ||
asset_id | TEXT | yes | References assets.id | ||
title | TEXT | no | Check: length(title) BETWEEN 5 AND 120 | ||
description | TEXT | yes | |||
requester_name | TEXT | no | |||
requester_email | TEXT | no | |||
status_token | TEXT | no | Unique | random; the status page is /s/<status_token> | |
status | TEXT | no | 'new' | Check: status IN ('new', 'converted', 'declined') | |
decline_reason | TEXT | yes | |||
work_order_id | TEXT | yes | the work order it became, or the one it was linked to as a duplicate. No foreign key: work_orders.request_id points back here (a cycle). | ||
created_at | TIMESTAMP | no |
- Table check:
(status = 'converted') = (work_order_id IS NOT NULL) - Table check:
(status = 'declined') = (decline_reason IS NOT NULL) - Index
requests_statuson(status) - App rule (R1): requester_email must be a valid email; a request from a QR code gets the asset and its location.
- App rule (R9): numbers are sequential (REQ-0001, REQ-0002...) and never reused.
request_photos
One photo attached to a request. Stores the request photos list.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
request_id | TEXT | no | Primary key (part); References requests.id, on delete cascade | ||
position | INTEGER | no | Primary key (part) | at most 5 photos per requestCheck: position BETWEEN 1 AND 5 | |
file_name | TEXT | no | Unique | the random name the image is stored under |
- App rule (section 13): images only, at most 10 MB each.
work_orders
A job to do: converted from a request, created by a schedule, or entered by staff.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
number | TEXT | no | Unique | e.g. WO-0105Check: substr(number, 1, 3) = 'WO-' | |
title | TEXT | no | |||
description | TEXT | yes | |||
location_id | TEXT | no | References locations.id | ||
asset_id | TEXT | yes | References assets.id | ||
priority | TEXT | no | Check: priority IN ('high', 'medium', 'low') | ||
assignee_id | TEXT | yes | References people.id | NULL when unassigned | |
due_on | DATE | no | overdue when not done and today is after this date | ||
status | TEXT | no | 'open' | Check: status IN ('open', 'in_progress', 'on_hold', 'done') | |
hold_reason | TEXT | yes | e.g. waiting for part | ||
completion_note | TEXT | yes | required by the Complete action | ||
completed_at | TIMESTAMP | yes | |||
request_id | TEXT | yes | References requests.id; Unique | the request it was converted from | |
schedule_id | TEXT | yes | References schedules.id | the schedule that created it | |
created_by | TEXT | yes | References people.id | NULL when the daily job created it from a schedule | |
created_at | TIMESTAMP | no |
- Table check:
status <> 'on_hold' OR hold_reason IS NOT NULL - Table check:
status <> 'done' OR completed_at IS NOT NULL - Unique index
work_orders_one_open_per_scheduleon(schedule_id)wherestatus <> 'done' AND schedule_id IS NOT NULL. Section 7: a schedule never has two work orders that are not done. - Index
work_orders_assigneeon(assignee_id, status) - Index
work_orders_dueon(status, due_on) - App rule (section 6): status moves only by Start, Hold, Resume, Complete and Reopen; Complete needs a completion note and every checklist item done or not applicable.
- App rule (R2): a done work order is read-only except an admin reopening it within 7 calendar days of the completion date.
- App rule (R3): due_on is today plus the priority due days from settings unless set by hand.
- App rule (R9): numbers are sequential (WO-0001, WO-0002...) and never reused.
checklist_items
One checklist item on one work order, copied from a template or added to the work order.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
work_order_id | TEXT | no | Primary key (part); References work_orders.id, on delete cascade | ||
position | INTEGER | no | Primary key (part) | Check: position >= 1 | |
label | TEXT | no | |||
kind | TEXT | no | Check: kind IN ('check', 'number', 'pass_fail') | ||
unit | TEXT | yes | |||
min | NUMERIC | yes | |||
max | NUMERIC | yes | |||
value | TEXT | yes | the reading: a decimal number for number items, pass or fail for pass_fail items | ||
done | BOOLEAN | no | FALSE | ||
flagged | BOOLEAN | no | FALSE | ||
na_reason | TEXT | yes | set when the item is marked not applicable |
- Table check:
kind = 'number' OR (unit IS NULL AND min IS NULL AND max IS NULL) - Table check:
min IS NULL OR max IS NULL OR min <= max - Table check:
kind <> 'pass_fail' OR value IS NULL OR value IN ('pass', 'fail') - Table check:
kind = 'number' OR flagged = FALSE - App rule (R4): a number reading outside min to max sets flagged and offers a follow-up work order.
time_entries
Minutes one person spent on one work order.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
work_order_id | TEXT | no | References work_orders.id, on delete cascade | ||
person_id | TEXT | no | References people.id | ||
minutes | INTEGER | no | whole minutesCheck: minutes > 0 | ||
note | TEXT | yes |
- Index
time_entries_work_orderon(work_order_id)
parts_used
A part used on one work order. Cost is quantity times unit_cost_cents.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
work_order_id | TEXT | no | References work_orders.id, on delete cascade | ||
name | TEXT | no | |||
quantity | INTEGER | no | Check: quantity > 0 | ||
unit_cost_cents | INTEGER | no | may be zeroCheck: unit_cost_cents >= 0 |
- Index
parts_used_work_orderon(work_order_id)
comments
A comment on a work order. Only public comments appear on the requester status page.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
work_order_id | TEXT | no | References work_orders.id, on delete cascade | ||
author_id | TEXT | no | References people.id | ||
body | TEXT | no | |||
at | TIMESTAMP | no | |||
public | BOOLEAN | no | FALSE | TRUE when the author ticked Show to the requester |
- Index
comments_work_orderon(work_order_id)
comment_photos
One photo attached to a comment. Stores the comment photos list.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
comment_id | TEXT | no | Primary key (part); References comments.id, on delete cascade | ||
position | INTEGER | no | Primary key (part) | at most 5 photos per commentCheck: position BETWEEN 1 AND 5 | |
file_name | TEXT | no | Unique | the random name the image is stored under |
- App rule (section 13): images only, at most 10 MB each.
events
One change, written by every action. Append-only.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
at | TIMESTAMP | no | |||
actor | TEXT | no | who made the change: a person id, the requester, or the daily job | ||
type | TEXT | no | e.g. request_converted, work_order_completed | ||
data | TEXT | yes | details of the change as JSON text |
- App rule (R10): events are never edited or deleted.
notifications
One email in the outbox. Every email is written here first, then sent.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
kind | TEXT | no | which trigger in section 10, e.g. request_received, assigned, overdue_digest | ||
to_email | TEXT | no | |||
subject | TEXT | no | |||
body | TEXT | no | |||
status | TEXT | no | 'queued' | logged when no email provider is configuredCheck: status IN ('queued', 'sent', 'failed', 'logged') | |
error | TEXT | yes |
- App rule (R8): a failed email never blocks the action that caused it.
The SQL
The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download maintenance-requests.sql or copy it from here.
-- Maintenance Requests database schema
-- Source: https://personalsoftwarefactory.com/blueprints/maintenance-requests (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.
-- 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),
org_name TEXT NOT NULL,
time_zone TEXT NOT NULL DEFAULT 'America/Chicago', -- IANA name; today, due dates and reports use this zone
due_days_high INTEGER NOT NULL DEFAULT 0 CHECK (due_days_high >= 0), -- days from today to due_on for a high priority work order (0 = due today)
due_days_medium INTEGER NOT NULL DEFAULT 3 CHECK (due_days_medium >= 0),
due_days_low INTEGER NOT NULL DEFAULT 7 CHECK (due_days_low >= 0),
pm_lead_days INTEGER NOT NULL DEFAULT 7 CHECK (pm_lead_days >= 0) -- a schedule creates its work order this many days before next_due_on
);
-- A staff member with an account. Requesters have no account and are not people.
-- App rule (section 13): deactivating a technician unassigns their open work orders and their schedules.
CREATE TABLE people (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase
role TEXT NOT NULL CHECK (role IN ('admin', 'technician')),
active BOOLEAN NOT NULL DEFAULT TRUE
);
-- A building or site, such as one restaurant. The public request form is /r/<id>.
CREATE TABLE locations (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
address TEXT
);
-- A piece of equipment at a location, with a QR code that opens the request form for it.
-- App rule (section 13): deactivating an asset pauses its schedules.
CREATE TABLE assets (
id TEXT PRIMARY KEY,
location_id TEXT NOT NULL REFERENCES locations(id),
name TEXT NOT NULL,
category TEXT,
make_model TEXT, -- free text, may be blank
serial_number TEXT, -- the spec field serial; renamed because SERIAL is a PostgreSQL type keyword. Free text, may be blank
qr_code TEXT NOT NULL UNIQUE, -- random token; the QR label opens /a/<qr_code>
active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE INDEX assets_location ON assets (location_id);
-- A named checklist that schedules and work orders copy their items from.
CREATE TABLE checklist_templates (
id TEXT PRIMARY KEY,
name TEXT NOT NULL
);
-- One item of a checklist template, in order. Stores the template items list.
CREATE TABLE checklist_template_items (
template_id TEXT NOT NULL REFERENCES checklist_templates(id) ON DELETE CASCADE,
position INTEGER NOT NULL CHECK (position >= 1),
label TEXT NOT NULL,
kind TEXT NOT NULL CHECK (kind IN ('check', 'number', 'pass_fail')),
unit TEXT, -- number items only, e.g. degrees F
min NUMERIC, -- number items only: a reading below this is flagged
max NUMERIC, -- number items only: a reading above this is flagged
PRIMARY KEY (template_id, position),
CHECK (kind = 'number' OR (unit IS NULL AND min IS NULL AND max IS NULL)),
CHECK (min IS NULL OR max IS NULL OR min <= max)
);
-- A preventive maintenance schedule that creates work orders ahead of each due date.
-- App rule (section 7): the daily job creates the next work order pm_lead_days before next_due_on; next_due_on moves only when the work order due on it is completed.
-- App rule (section 7): fixed schedules advance from the previous due date, after_completion schedules from the completion date; months are calendar months.
CREATE TABLE schedules (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
asset_id TEXT REFERENCES assets(id),
location_id TEXT NOT NULL REFERENCES locations(id),
checklist_template_id TEXT REFERENCES checklist_templates(id), -- optional; copied onto each work order
every_n INTEGER NOT NULL CHECK (every_n >= 1),
every_unit TEXT NOT NULL CHECK (every_unit IN ('days', 'weeks', 'months')),
mode TEXT NOT NULL CHECK (mode IN ('fixed', 'after_completion')),
next_due_on DATE NOT NULL,
assignee_id TEXT REFERENCES people(id), -- NULL when unassigned
priority TEXT NOT NULL CHECK (priority IN ('high', 'medium', 'low')),
active BOOLEAN NOT NULL DEFAULT TRUE -- FALSE when paused
);
-- A problem reported through the public form, waiting for triage or already handled.
-- App rule (R1): requester_email must be a valid email; a request from a QR code gets the asset and its location.
-- App rule (R9): numbers are sequential (REQ-0001, REQ-0002...) and never reused.
CREATE TABLE requests (
id TEXT PRIMARY KEY,
number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 4) = 'REQ-'), -- e.g. REQ-0032
location_id TEXT NOT NULL REFERENCES locations(id),
asset_id TEXT REFERENCES assets(id),
title TEXT NOT NULL CHECK (length(title) BETWEEN 5 AND 120),
description TEXT,
requester_name TEXT NOT NULL,
requester_email TEXT NOT NULL,
status_token TEXT NOT NULL UNIQUE, -- random; the status page is /s/<status_token>
status TEXT NOT NULL DEFAULT 'new' CHECK (status IN ('new', 'converted', 'declined')),
decline_reason TEXT,
work_order_id TEXT, -- the work order it became, or the one it was linked to as a duplicate. No foreign key: work_orders.request_id points back here (a cycle).
created_at TIMESTAMP NOT NULL,
CHECK ((status = 'converted') = (work_order_id IS NOT NULL)),
CHECK ((status = 'declined') = (decline_reason IS NOT NULL))
);
CREATE INDEX requests_status ON requests (status);
-- One photo attached to a request. Stores the request photos list.
-- App rule (section 13): images only, at most 10 MB each.
CREATE TABLE request_photos (
request_id TEXT NOT NULL REFERENCES requests(id) ON DELETE CASCADE,
position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), -- at most 5 photos per request
file_name TEXT NOT NULL UNIQUE, -- the random name the image is stored under
PRIMARY KEY (request_id, position)
);
-- A job to do: converted from a request, created by a schedule, or entered by staff.
-- App rule (section 6): status moves only by Start, Hold, Resume, Complete and Reopen; Complete needs a completion note and every checklist item done or not applicable.
-- App rule (R2): a done work order is read-only except an admin reopening it within 7 calendar days of the completion date.
-- App rule (R3): due_on is today plus the priority due days from settings unless set by hand.
-- App rule (R9): numbers are sequential (WO-0001, WO-0002...) and never reused.
CREATE TABLE work_orders (
id TEXT PRIMARY KEY,
number TEXT NOT NULL UNIQUE CHECK (substr(number, 1, 3) = 'WO-'), -- e.g. WO-0105
title TEXT NOT NULL,
description TEXT,
location_id TEXT NOT NULL REFERENCES locations(id),
asset_id TEXT REFERENCES assets(id),
priority TEXT NOT NULL CHECK (priority IN ('high', 'medium', 'low')),
assignee_id TEXT REFERENCES people(id), -- NULL when unassigned
due_on DATE NOT NULL, -- overdue when not done and today is after this date
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'in_progress', 'on_hold', 'done')),
hold_reason TEXT, -- e.g. waiting for part
completion_note TEXT, -- required by the Complete action
completed_at TIMESTAMP,
request_id TEXT UNIQUE REFERENCES requests(id), -- the request it was converted from
schedule_id TEXT REFERENCES schedules(id), -- the schedule that created it
created_by TEXT REFERENCES people(id), -- NULL when the daily job created it from a schedule
created_at TIMESTAMP NOT NULL,
CHECK (status <> 'on_hold' OR hold_reason IS NOT NULL),
CHECK (status <> 'done' OR completed_at IS NOT NULL)
);
-- Section 7: a schedule never has two work orders that are not done.
CREATE UNIQUE INDEX work_orders_one_open_per_schedule ON work_orders (schedule_id) WHERE status <> 'done' AND schedule_id IS NOT NULL;
CREATE INDEX work_orders_assignee ON work_orders (assignee_id, status);
CREATE INDEX work_orders_due ON work_orders (status, due_on);
-- One checklist item on one work order, copied from a template or added to the work order.
-- App rule (R4): a number reading outside min to max sets flagged and offers a follow-up work order.
CREATE TABLE checklist_items (
work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
position INTEGER NOT NULL CHECK (position >= 1),
label TEXT NOT NULL,
kind TEXT NOT NULL CHECK (kind IN ('check', 'number', 'pass_fail')),
unit TEXT,
min NUMERIC,
max NUMERIC,
value TEXT, -- the reading: a decimal number for number items, pass or fail for pass_fail items
done BOOLEAN NOT NULL DEFAULT FALSE,
flagged BOOLEAN NOT NULL DEFAULT FALSE,
na_reason TEXT, -- set when the item is marked not applicable
PRIMARY KEY (work_order_id, position),
CHECK (kind = 'number' OR (unit IS NULL AND min IS NULL AND max IS NULL)),
CHECK (min IS NULL OR max IS NULL OR min <= max),
CHECK (kind <> 'pass_fail' OR value IS NULL OR value IN ('pass', 'fail')),
CHECK (kind = 'number' OR flagged = FALSE) -- only number items are flagged
);
-- Minutes one person spent on one work order.
CREATE TABLE time_entries (
id TEXT PRIMARY KEY,
work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
person_id TEXT NOT NULL REFERENCES people(id),
minutes INTEGER NOT NULL CHECK (minutes > 0), -- whole minutes
note TEXT
);
CREATE INDEX time_entries_work_order ON time_entries (work_order_id);
-- A part used on one work order. Cost is quantity times unit_cost_cents.
CREATE TABLE parts_used (
id TEXT PRIMARY KEY,
work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
name TEXT NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
unit_cost_cents INTEGER NOT NULL CHECK (unit_cost_cents >= 0) -- may be zero
);
CREATE INDEX parts_used_work_order ON parts_used (work_order_id);
-- A comment on a work order. Only public comments appear on the requester status page.
CREATE TABLE comments (
id TEXT PRIMARY KEY,
work_order_id TEXT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
author_id TEXT NOT NULL REFERENCES people(id),
body TEXT NOT NULL,
at TIMESTAMP NOT NULL,
public BOOLEAN NOT NULL DEFAULT FALSE -- TRUE when the author ticked Show to the requester
);
CREATE INDEX comments_work_order ON comments (work_order_id);
-- One photo attached to a comment. Stores the comment photos list.
-- App rule (section 13): images only, at most 10 MB each.
CREATE TABLE comment_photos (
comment_id TEXT NOT NULL REFERENCES comments(id) ON DELETE CASCADE,
position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), -- at most 5 photos per comment
file_name TEXT NOT NULL UNIQUE, -- the random name the image is stored under
PRIMARY KEY (comment_id, position)
);
-- One change, written by every action. Append-only.
-- App rule (R10): events are never edited or deleted.
CREATE TABLE events (
id TEXT PRIMARY KEY,
at TIMESTAMP NOT NULL,
actor TEXT NOT NULL, -- who made the change: a person id, the requester, or the daily job
type TEXT NOT NULL, -- e.g. request_converted, work_order_completed
data TEXT -- details of the change as JSON text
);
-- One email in the outbox. Every email is written here first, then sent.
-- App rule (R8): a failed email never blocks the action that caused it.
CREATE TABLE notifications (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL, -- which trigger in section 10, e.g. request_received, assigned, overdue_digest
to_email TEXT NOT NULL,
subject TEXT NOT NULL,
body TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged when no email provider is configured
error TEXT
);Seed data
Loads section 14 as of the spec's clock date, Thursday 2026-10-15: settings, 3 people, 2 locations, 5 assets, 2 checklist templates with 6 items, 4 schedules, 2 requests, 11 work orders (WO-0094 to WO-0104), the 7 checklist items the spec lists on WO-0100, WO-0102 and WO-0103, and WO-0104's 50 minutes and Latch kit. Times are 12:00 UTC because the spec gives dates only. Left out or filled by rule: qr_code and status_token are readable placeholders for the random tokens the app generates; asset categories, descriptions and completion notes are NULL because the spec gives none; who converted REQ-0030 and who created WO-0103 and WO-0104 is not given, so created_by is NULL; WO-0104's minutes are logged to its assignee, Raj; WO-0097's created date, priority and assignee follow the Clean ice machine schedule's rules; the paused weekly schedule's next due date, Oct 1, follows the fixed rule from its last due date, Sep 24. The September work orders have no checklist rows because the spec gives no readings. No comments, photos, events or emails are seeded.
Download maintenance-requests-seed.sql (45 rows). It loads with foreign keys enforced on both engines.
Load it
SQLite, into a new file:
curl -s https://personalsoftwarefactory.com/schemas/maintenance-requests.sql https://personalsoftwarefactory.com/schemas/maintenance-requests-seed.sql | sqlite3 maintenance_requests.dbPostgreSQL, into a new database:
createdb maintenance_requests && curl -s https://personalsoftwarefactory.com/schemas/maintenance-requests.sql https://personalsoftwarefactory.com/schemas/maintenance-requests-seed.sql | psql -v ON_ERROR_STOP=1 maintenance_requestsSQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.
Design decisions
Request and work order point at each other, with one foreign key
From the build test · field report
A request records the work order it became (section 5), and a work order records the request it was converted from, which is a cycle. The foreign key is on work_orders.request_id, which is also unique because a request converts once; requests.work_order_id is a plain column. The build test found that a request linked to an existing work order as a duplicate had no status; it becomes converted with that work order, so several requests can share one work_order_id while only one is the work order's request_id. Two CHECKs make status converted exactly when work_order_id is set and declined exactly when decline_reason is set.
A schedule has at most one work order that is not done
From the build test · field report
Section 7 says a schedule never has two open work orders, so a partial unique index on work_orders.schedule_id covers rows WHERE status <> 'done'. The build test found section 7 could be read as moving next_due_on when the work order is created; next_due_on now stays until the work order due on it is completed, so it is a plain date the daily job updates, and the Next due flag is computed from it rather than stored.
Checklists are copied onto the work order
From the build test · field report
ChecklistItem is per work order (section 5), so each work order gets its own rows with the label, kind, unit and range copied from the template, plus value, done, flagged and na_reason. Editing a template then never changes a reading already taken. The build test found nothing said whether a Fail on a pass/fail item is flagged; it is recorded but not flagged, so a CHECK allows flagged only on number items.
Comments are private unless marked public
From the build test · field report
The build test found status pages showed public comments but Comment had no public field. comments.public is a BOOLEAN that defaults to FALSE and is set by Show to the requester; the status page query filters on it, so an internal note never reaches a requester by default.
Photos are child rows with a position from 1 to 5
From the build test · field report
Requests and comments take up to 5 photos (section 13), so each photo is a row with a position CHECKed between 1 and 5 and part of the primary key, which caps the count in the database. The build test found photos had no allowed types or access rule; only images are accepted and each is stored under a random name, which is the unique file_name column. The image type and the 10 MB limit are checked by the app.
Done needs a completion time; the note is checked by the action
A CHECK requires completed_at whenever status is done, and hold_reason whenever status is on_hold. The Complete action also requires a completion note and a finished checklist (section 6), but that stays an app rule: the seeded past work orders are done without a note in the spec, and the checklist rule spans rows.
serial is stored as serial_number
The spec names the asset field serial, but SERIAL is a PostgreSQL column type keyword and a common source of confusion in portable SQL, so the column is serial_number with a comment naming the spec field. Every other column keeps the spec's name, including at, min, max and public, which both engines accept as column names.
Example queries
The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.
Open tab: open and in-progress work, overdue first
SELECT w.number, w.title, w.status, w.priority, w.due_on,
COALESCE(p.name, 'Unassigned') AS assignee,
CASE WHEN w.due_on < '2026-10-15' THEN 'overdue' ELSE '' END AS flag
FROM work_orders w
LEFT JOIN people p ON p.id = w.assignee_id
WHERE w.status IN ('open', 'in_progress')
ORDER BY CASE WHEN w.due_on < '2026-10-15' THEN 0 ELSE 1 END,
CASE w.priority WHEN 'high' THEN 0 WHEN 'medium' THEN 1 ELSE 2 END,
w.due_on, w.number;On the seed data, with today as 2026-10-15: 4 rows; WO-0099 Change HVAC filters is first and overdue, then WO-0101, WO-0102 and WO-0100.
Requests waiting for triage
SELECT r.number, r.title, l.name AS location, COALESCE(a.name, '') AS asset,
r.requester_name, r.requester_email, r.created_at
FROM requests r
JOIN locations l ON l.id = r.location_id
LEFT JOIN assets a ON a.id = r.asset_id
WHERE r.status = 'new'
ORDER BY r.created_at, r.number;On the seed data: 1 row; REQ-0031 Mop sink drains slowly at Eastside, from Leo Park.
Requester status page
SELECT r.number, r.title, l.name AS location, COALESCE(a.name, '') AS asset,
CASE
WHEN r.status = 'new' THEN 'Received, waiting for the maintenance team'
WHEN r.status = 'declined' THEN 'Declined: ' || r.decline_reason
WHEN w.status = 'open' THEN 'Scheduled'
WHEN w.status = 'in_progress' THEN 'Being worked on'
WHEN w.status = 'on_hold' THEN 'On hold'
ELSE 'Fixed'
END AS shown_status,
COALESCE(w.number, '') AS work_order, w.due_on,
(SELECT COUNT(*) FROM comments c WHERE c.work_order_id = w.id AND c.public = TRUE) AS public_comments
FROM requests r
JOIN locations l ON l.id = r.location_id
LEFT JOIN assets a ON a.id = r.asset_id
LEFT JOIN work_orders w ON w.id = r.work_order_id
WHERE r.status_token = 'status-placeholder-0030';On the seed data, for REQ-0030: 1 row; Walk-in cooler warm shows Being worked on, work order WO-0101 due 2026-10-15, 0 public comments.
Cost and minutes by location for October
SELECT l.name AS location,
COALESCE((SELECT SUM(pu.quantity * pu.unit_cost_cents) FROM parts_used pu JOIN work_orders w ON w.id = pu.work_order_id
WHERE w.location_id = l.id AND w.created_at >= '2026-10-01 05:00:00' AND w.created_at < '2026-11-01 05:00:00'), 0) AS parts_cost_cents,
COALESCE((SELECT SUM(t.minutes) FROM time_entries t JOIN work_orders w ON w.id = t.work_order_id
WHERE w.location_id = l.id AND w.created_at >= '2026-10-01 05:00:00' AND w.created_at < '2026-11-01 05:00:00'), 0) AS minutes
FROM locations l
ORDER BY l.name;On the seed data, for work orders created in October 2026 in Chicago time (05:00 UTC boundaries): 2 rows; Downtown $0 and 0 minutes, Eastside 2,400 cents ($24.00) and 50 minutes.
Preventive schedules and their open work order
SELECT s.title, s.every_n, s.every_unit, s.mode, s.next_due_on,
CASE WHEN s.active = TRUE THEN 'active' ELSE 'paused' END AS state,
COALESCE(p.name, 'Unassigned') AS assignee,
COALESCE(w.number, '') AS open_work_order
FROM schedules s
LEFT JOIN people p ON p.id = s.assignee_id
LEFT JOIN work_orders w ON w.schedule_id = s.id AND w.status <> 'done'
ORDER BY s.next_due_on, s.title;On the seed data: 4 rows; Weekly walk-in check is paused with next due 2026-10-01, Change HVAC filters has WO-0099 open, and Hood inspection is next due 2027-01-15 with no open work order.
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 Maintenance Requests blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 17 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 17 of 17 acceptance checks in a browser.
- Read the Maintenance Requests 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.