# Inventory management system database schema

Source: https://personalsoftwarefactory.com/schemas/inventory-tracker
Updated: 2026-12-04

> Eleven tables. The design rests on movements: on-hand stock is never stored on the item, it is the sum of an append-only movements table, with CHECK constraints that tie each movement type to its sign, reason and source. Purchase orders, their lines and stock counts feed movements, and a partial unique index allows only one open or submitted count per location.

- 11 tables, 2 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1) on 2026-12-04
- 23 seed rows from the blueprint
- The blueprint's test build passed 21 of 21 acceptance checks

Files: [inventory-tracker.sql](https://personalsoftwarefactory.com/schemas/inventory-tracker.sql) · [inventory-tracker-seed.sql](https://personalsoftwarefactory.com/schemas/inventory-tracker-seed.sql) · [the Inventory Tracker blueprint](https://personalsoftwarefactory.com/blueprints/inventory-tracker)

## Relationships

- people.default_location_id references locations.id: each people row points to at most one locations row, and one locations row can have many people rows pointing to it.
- items.supplier_id references suppliers.id: each items row points to exactly one suppliers row, and one suppliers row can have many items rows pointing to it.
- purchase_orders.supplier_id references suppliers.id: each purchase_orders row points to exactly one suppliers row, and one suppliers row can have many purchase_orders rows pointing to it.
- purchase_orders.created_by references people.id: each purchase_orders row points to exactly one people row, and one people row can have many purchase_orders rows pointing to it.
- po_lines.po_id references purchase_orders.id: each po_lines row points to exactly one purchase_orders row, and one purchase_orders row can have many po_lines rows pointing to it.
- po_lines.item_id references items.id: each po_lines row points to exactly one items row, and one items row can have many po_lines rows pointing to it.
- counts.location_id references locations.id: each counts row points to exactly one locations row, and one locations row can have many counts rows pointing to it.
- counts.started_by references people.id: each counts row points to exactly one people row, and one people row can have many counts rows pointing to it.
- counts.submitted_by references people.id: each counts row points to at most one people row, and one people row can have many counts rows pointing to it.
- counts.approved_by references people.id: each counts row points to at most one people row, and one people row can have many counts rows pointing to it.
- count_lines.count_id references counts.id: each count_lines row points to exactly one counts row, and one counts row can have many count_lines rows pointing to it.
- count_lines.item_id references items.id: each count_lines row points to exactly one items row, and one items row can have many count_lines rows pointing to it.
- movements.item_id references items.id: each movements row points to exactly one items row, and one items row can have many movements rows pointing to it.
- movements.location_id references locations.id: each movements row points to exactly one locations row, and one locations row can have many movements rows pointing to it.
- movements.po_line_id references po_lines.id: each movements row points to at most one po_lines row, and one po_lines row can have many movements rows pointing to it.
- movements.count_id references counts.id: each movements row points to at most one counts row, and one counts row can have many movements rows pointing to it.
- movements.person_id references people.id: each movements row points to exactly one people row, and one people row can have many movements rows pointing to it.
- events.actor 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.

## Tables

Also described as: stock control database design, inventory database tables, purchase order and stock movement schema.

### locations

A place where stock is kept, such as the store room or a van, with the short code printed on its label. Implements: Location.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  | Unique |  |
| `code` | TEXT | no |  | Unique | used in location labels (/l/<code>); up to 12 letters, digits or hyphens, stored in capitals |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- Table check: `code = upper(code) AND length(code) BETWEEN 1 AND 12`
- App rule (R8): a location is deactivated only when empty and when it is no active person's default location.

### people

Someone who can sign in, with their role and the location their Use button starts at. Implements: Person.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  | Unique | stored lowercase |
| `role` | TEXT | no |  |  | Check: `role IN ('admin', 'staff')` |
| `default_location_id` | TEXT | yes |  | References locations.id | where Use starts on a phone; NULL for admins in the seed |
| `active` | BOOLEAN | no | `TRUE` |  |  |

- Table check: `email = lower(email)`
- App rule (section 4): the email must be at the company domain; admins cannot demote or deactivate themselves; at least one active admin.

### suppliers

A supplier that purchase orders are emailed to, with its lead time in days. Implements: Supplier.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `name` | TEXT | no |  |  |  |
| `email` | TEXT | no |  |  | purchase orders are emailed here |
| `lead_time_days` | INTEGER | no |  |  | sets the default expected date on Send Check: `lead_time_days BETWEEN 0 AND 365` |

### items

A stocked item with its SKU, unit, latest unit cost, reorder point, reorder quantity and supplier. Implements: Item.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `sku` | TEXT | no |  | Unique | letters, digits, dots, underscores, hyphens; stored in capitals so uniqueness ignores case |
| `name` | TEXT | no |  |  |  |
| `unit` | TEXT | no |  |  | one unit per item: bottle, box, case, each |
| `barcode` | TEXT | yes |  | Unique | optional, no spaces |
| `photo` | TEXT | yes |  |  | optional; a file path or URL |
| `unit_cost_cents` | INTEGER | no |  |  | latest cost; set by each purchase order receipt (R6) Check: `unit_cost_cents >= 0` |
| `reorder_point` | INTEGER | no | `0` |  | low when total on hand is at or below this (R4) Check: `reorder_point >= 0` |
| `reorder_qty` | INTEGER | no | `0` |  | 0 means never suggested Check: `reorder_qty >= 0` |
| `supplier_id` | TEXT | no |  | References suppliers.id |  |
| `low_alerted` | BOOLEAN | no | `FALSE` |  | TRUE after a low stock email, until the total rises above reorder_point |
| `archived` | BOOLEAN | no | `FALSE` |  |  |

- Table check: `sku = upper(sku)`
- Table check: `barcode IS NULL OR barcode NOT LIKE '% %'`
- App rule (R8): archive only with nothing on hand anywhere and nothing on sent or partially received orders.
- App rule (R10): unit cannot change once the item has any movement.

### purchase_orders

An order to one supplier, from draft to received or cancelled. Implements: PurchaseOrder.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | TEXT | no |  | Unique | PO-0001 |
| `supplier_id` | TEXT | no |  | References suppliers.id | cannot change after creation |
| `status` | TEXT | no | `'draft'` |  | Check: `status IN ('draft', 'sent', 'partially_received', 'received', 'cancelled')` |
| `expected_date` | DATE | yes |  |  | set on Send: today plus lead time by default, never in the past, fixed once sent |
| `sent_at` | TIMESTAMP | yes |  |  |  |
| `closed_short` | BOOLEAN | no | `FALSE` |  | TRUE when an admin closed it with quantities outstanding |
| `notes` | TEXT | yes |  |  |  |
| `created_by` | TEXT | no |  | References people.id |  |

- Table check: `closed_short = FALSE OR status = 'received'`
- App rule (section 7): status moves draft > sent > partially_received > received, or to cancelled from draft, or from sent with nothing received.

### po_lines

One item on a purchase order, with the quantity ordered, the quantity received so far and the unit cost. Implements: POLine.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `po_id` | TEXT | no |  | References purchase_orders.id |  |
| `item_id` | TEXT | no |  | References items.id |  |
| `qty_ordered` | INTEGER | no |  |  | Check: `qty_ordered > 0` |
| `qty_received` | INTEGER | no | `0` |  | never more than ordered: receiving more than is outstanding is refused |
| `unit_cost_cents` | INTEGER | no |  |  | Check: `unit_cost_cents >= 0` |

- Unique together: `po_id`, `item_id`
- Table check: `qty_received BETWEEN 0 AND qty_ordered`

### counts

A stock count of one location, from open to approved or cancelled. Implements: Count.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `number` | TEXT | no |  | Unique | C-0001 |
| `location_id` | TEXT | no |  | References locations.id |  |
| `status` | TEXT | no | `'open'` |  | Check: `status IN ('open', 'submitted', 'approved', 'cancelled')` |
| `started_at` | TIMESTAMP | no |  |  |  |
| `started_by` | TEXT | no |  | References people.id |  |
| `submitted_by` | TEXT | yes |  | References people.id |  |
| `approved_by` | TEXT | yes |  | References people.id | must be an admin (app rule) |

- Unique index `counts_one_active_per_location` on (location_id) where `status IN ('open', 'submitted')`. At most one open or submitted count per location (section 13).
- App rule (R7): counts are blind; expected quantities are hidden while open and never shown to staff.
- App rule (R7): approval is refused at an inactive location or when an archived item's count differs.

### count_lines

One item on a count: the quantity expected when the count started and the quantity counted. Implements: CountLine.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `count_id` | TEXT | no |  | Primary key (part); References counts.id |  |
| `item_id` | TEXT | no |  | Primary key (part); References items.id |  |
| `expected` | INTEGER | no |  |  | on hand at this location when the count started |
| `counted` | INTEGER | yes |  |  | NULL until entered; blank is not 0 Check: `counted >= 0` |
| `changed_during_count` | BOOLEAN | no | `FALSE` |  | set when the item moves at this location after the count started; stays set |

- App rule (R7): a flagged line must be recounted or confirmed by the approver before approval.

### movements

One signed change to one item's quantity at one location; the sum of movements is the stock on hand. Implements: Movement.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `at` | TIMESTAMP | no |  |  | count movements are dated at approval |
| `item_id` | TEXT | no |  | References items.id |  |
| `location_id` | TEXT | no |  | References locations.id |  |
| `quantity` | INTEGER | no |  |  | positive in, negative out, in the item's unit |
| `type` | TEXT | no |  |  | Check: `type IN ('opening', 'receive', 'use', 'move', 'adjust', 'count')` |
| `reason` | TEXT | yes |  |  | required for adjust Check: `reason IN ('Damaged', 'Lost', 'Found', 'Correction', 'Opening balance')` |
| `po_line_id` | TEXT | yes |  | References po_lines.id | set on receive |
| `count_id` | TEXT | yes |  | References counts.id | set on count |
| `transfer_id` | TEXT | yes |  |  | joins the two sides of a move |
| `person_id` | TEXT | no |  | References people.id | for count movements, the approver |

- Table check: `quantity <> 0 OR type = 'opening'`
- Table check: `type <> 'opening' OR quantity >= 0`
- Table check: `type <> 'use' OR quantity < 0`
- Table check: `type <> 'receive' OR (quantity > 0 AND po_line_id IS NOT NULL)`
- Table check: `type <> 'move' OR transfer_id IS NOT NULL`
- Table check: `type <> 'count' OR count_id IS NOT NULL`
- Table check: `type <> 'adjust' OR reason IS NOT NULL`
- Table check: `reason IS NULL OR reason NOT IN ('Damaged', 'Lost') OR quantity < 0`
- Table check: `reason IS NULL OR reason <> 'Found' OR quantity > 0`
- Index `movements_item_location` on (item_id, location_id)

### events

An append-only audit entry: who did what, when, with details. Implements: Event.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `at` | TIMESTAMP | no |  |  |  |
| `actor` | TEXT | yes |  | References people.id | NULL when the daily job acted |
| `type` | TEXT | no |  |  |  |
| `data` | TEXT | yes |  |  | details as JSON text |

### notifications

An email in the outbox, written before it is sent, with its status. Implements: Notification.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `kind` | TEXT | no |  |  | which email it is: one of the triggers in section 10 |
| `to_email` | TEXT | no |  |  |  |
| `subject` | TEXT | no |  |  |  |
| `body` | TEXT | no |  |  |  |
| `status` | TEXT | no | `'queued'` |  | logged when no provider is configured Check: `status IN ('queued', 'sent', 'failed', 'logged')` |
| `error` | TEXT | yes |  |  |  |
| `written_at` | TIMESTAMP | no |  |  | when the email was written; shown on the Outbox |

## The SQL

```sql
-- Inventory Tracker database schema
-- Source: https://personalsoftwarefactory.com/blueprints/inventory-tracker (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 place where stock is kept: a store room, a van, a shelf, a site.
-- App rule (R8): a location is deactivated only when empty and when it is no active person's default location.
CREATE TABLE locations (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  code TEXT NOT NULL UNIQUE, -- used in location labels (/l/<code>); up to 12 letters, digits or hyphens, stored in capitals
  active BOOLEAN NOT NULL DEFAULT TRUE,
  CHECK (code = upper(code) AND length(code) BETWEEN 1 AND 12)
);

-- A person who can sign in. Never deleted, only deactivated.
-- App rule (section 4): the email must be at the company domain; admins cannot demote or deactivate themselves; at least one active admin.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE, -- stored lowercase
  role TEXT NOT NULL CHECK (role IN ('admin', 'staff')),
  default_location_id TEXT REFERENCES locations(id), -- where Use starts on a phone; NULL for admins in the seed
  active BOOLEAN NOT NULL DEFAULT TRUE,
  CHECK (email = lower(email))
);

-- A supplier that purchase orders are emailed to.
CREATE TABLE suppliers (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT NOT NULL, -- purchase orders are emailed here
  lead_time_days INTEGER NOT NULL CHECK (lead_time_days BETWEEN 0 AND 365) -- sets the default expected date on Send
);

-- A stocked item. Archived, never deleted.
-- App rule (R8): archive only with nothing on hand anywhere and nothing on sent or partially received orders.
-- App rule (R10): unit cannot change once the item has any movement.
CREATE TABLE items (
  id TEXT PRIMARY KEY,
  sku TEXT NOT NULL UNIQUE, -- letters, digits, dots, underscores, hyphens; stored in capitals so uniqueness ignores case
  name TEXT NOT NULL,
  unit TEXT NOT NULL, -- one unit per item: bottle, box, case, each
  barcode TEXT UNIQUE, -- optional, no spaces
  photo TEXT, -- optional; a file path or URL
  unit_cost_cents INTEGER NOT NULL CHECK (unit_cost_cents >= 0), -- latest cost; set by each purchase order receipt (R6)
  reorder_point INTEGER NOT NULL DEFAULT 0 CHECK (reorder_point >= 0), -- low when total on hand is at or below this (R4)
  reorder_qty INTEGER NOT NULL DEFAULT 0 CHECK (reorder_qty >= 0), -- 0 means never suggested
  supplier_id TEXT NOT NULL REFERENCES suppliers(id),
  low_alerted BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE after a low stock email, until the total rises above reorder_point
  archived BOOLEAN NOT NULL DEFAULT FALSE,
  CHECK (sku = upper(sku)),
  CHECK (barcode IS NULL OR barcode NOT LIKE '% %')
);

-- A purchase order to one supplier. Numbers run PO-0001, PO-0002, one above the highest; cancelled orders keep theirs.
-- App rule (section 7): status moves draft > sent > partially_received > received, or to cancelled from draft, or from sent with nothing received.
CREATE TABLE purchase_orders (
  id TEXT PRIMARY KEY,
  number TEXT NOT NULL UNIQUE, -- PO-0001
  supplier_id TEXT NOT NULL REFERENCES suppliers(id), -- cannot change after creation
  status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'sent', 'partially_received', 'received', 'cancelled')),
  expected_date DATE, -- set on Send: today plus lead time by default, never in the past, fixed once sent
  sent_at TIMESTAMP,
  closed_short BOOLEAN NOT NULL DEFAULT FALSE, -- TRUE when an admin closed it with quantities outstanding
  notes TEXT,
  created_by TEXT NOT NULL REFERENCES people(id),
  CHECK (closed_short = FALSE OR status = 'received')
);

-- One item on a purchase order. One line per item per order.
CREATE TABLE po_lines (
  id TEXT PRIMARY KEY,
  po_id TEXT NOT NULL REFERENCES purchase_orders(id),
  item_id TEXT NOT NULL REFERENCES items(id),
  qty_ordered INTEGER NOT NULL CHECK (qty_ordered > 0),
  qty_received INTEGER NOT NULL DEFAULT 0, -- never more than ordered: receiving more than is outstanding is refused
  unit_cost_cents INTEGER NOT NULL CHECK (unit_cost_cents >= 0),
  UNIQUE (po_id, item_id),
  CHECK (qty_received BETWEEN 0 AND qty_ordered)
);

-- A stock count of one location. Numbers run C-0001, C-0002.
-- App rule (R7): counts are blind; expected quantities are hidden while open and never shown to staff.
-- App rule (R7): approval is refused at an inactive location or when an archived item's count differs.
CREATE TABLE counts (
  id TEXT PRIMARY KEY,
  number TEXT NOT NULL UNIQUE, -- C-0001
  location_id TEXT NOT NULL REFERENCES locations(id),
  status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'submitted', 'approved', 'cancelled')),
  started_at TIMESTAMP NOT NULL,
  started_by TEXT NOT NULL REFERENCES people(id),
  submitted_by TEXT REFERENCES people(id),
  approved_by TEXT REFERENCES people(id) -- must be an admin (app rule)
);

-- At most one open or submitted count per location (section 13).
CREATE UNIQUE INDEX counts_one_active_per_location ON counts (location_id) WHERE status IN ('open', 'submitted');

-- One item on a count.
-- App rule (R7): a flagged line must be recounted or confirmed by the approver before approval.
CREATE TABLE count_lines (
  count_id TEXT NOT NULL REFERENCES counts(id),
  item_id TEXT NOT NULL REFERENCES items(id),
  expected INTEGER NOT NULL, -- on hand at this location when the count started
  counted INTEGER CHECK (counted >= 0), -- NULL until entered; blank is not 0
  changed_during_count BOOLEAN NOT NULL DEFAULT FALSE, -- set when the item moves at this location after the count started; stays set
  PRIMARY KEY (count_id, item_id)
);

-- One change to the quantity of one item at one location. Append-only: never edited or deleted (R2).
-- On hand at a location is the sum of its movements. App rule (R1): no movement may take a location below zero.
CREATE TABLE movements (
  id TEXT PRIMARY KEY,
  at TIMESTAMP NOT NULL, -- count movements are dated at approval
  item_id TEXT NOT NULL REFERENCES items(id),
  location_id TEXT NOT NULL REFERENCES locations(id),
  quantity INTEGER NOT NULL, -- positive in, negative out, in the item's unit
  type TEXT NOT NULL CHECK (type IN ('opening', 'receive', 'use', 'move', 'adjust', 'count')),
  reason TEXT CHECK (reason IN ('Damaged', 'Lost', 'Found', 'Correction', 'Opening balance')), -- required for adjust
  po_line_id TEXT REFERENCES po_lines(id), -- set on receive
  count_id TEXT REFERENCES counts(id), -- set on count
  transfer_id TEXT, -- joins the two sides of a move
  person_id TEXT NOT NULL REFERENCES people(id), -- for count movements, the approver
  CHECK (quantity <> 0 OR type = 'opening'), -- only opening balances may be 0
  CHECK (type <> 'opening' OR quantity >= 0),
  CHECK (type <> 'use' OR quantity < 0),
  CHECK (type <> 'receive' OR (quantity > 0 AND po_line_id IS NOT NULL)),
  CHECK (type <> 'move' OR transfer_id IS NOT NULL),
  CHECK (type <> 'count' OR count_id IS NOT NULL),
  CHECK (type <> 'adjust' OR reason IS NOT NULL),
  CHECK (reason IS NULL OR reason NOT IN ('Damaged', 'Lost') OR quantity < 0), -- Damaged and Lost only remove
  CHECK (reason IS NULL OR reason <> 'Found' OR quantity > 0) -- Found only adds
);

CREATE INDEX movements_item_location ON movements (item_id, location_id);

-- Audit log entry. Append-only (R2). Unit cost changes (date, old and new cost, source) are read from here.
CREATE TABLE events (
  id TEXT PRIMARY KEY,
  at TIMESTAMP NOT NULL,
  actor TEXT REFERENCES people(id), -- NULL when the daily job acted
  type TEXT NOT NULL,
  data TEXT -- details as JSON text
);

-- An email in the outbox. Written here first, then sent.
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL, -- which email it is: one of the triggers in section 10
  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 provider is configured
  error TEXT,
  written_at TIMESTAMP NOT NULL -- when the email was written; shown on the Outbox
);
```

## Seed data

Loads Brightside Cleaning Co. from section 14: 3 locations, 4 people, 2 suppliers, 8 items (nitrile gloves with low_alerted set, as the spec says their email already went out), and purchase orders PO-0006 (received) and PO-0007 (sent) with their 4 lines. The outbox starts empty, as the spec says. The movement history is left out: the spec gives each item's total use (glass cleaner 18, nitrile gloves 9 and so on) but not how it splits between Van 1 and Van 2 or on which days, and the opening balances are chosen from that split, so any history would be invented. On this seed every on-hand quantity is therefore 0; to reach the section 14 stock table, record one opening movement per item and location with the table's quantity. The spec gives days, not times, so sent_at values are 00:00 UTC on the day.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/inventory-tracker.sql https://personalsoftwarefactory.com/schemas/inventory-tracker-seed.sql | sqlite3 inventory_tracker.db
# PostgreSQL
createdb inventory_tracker && curl -s https://personalsoftwarefactory.com/schemas/inventory-tracker.sql https://personalsoftwarefactory.com/schemas/inventory-tracker-seed.sql | psql -v ON_ERROR_STOP=1 inventory_tracker
```

## Design decisions

### On-hand stock is the sum of movements

Section 5 makes the movement log the source of truth: the on-hand quantity is the sum of movements, and R2 says movements are never edited or deleted. So items has no quantity column. The spec allows a balance table for speed if it is updated in the same transaction as each movement; this schema leaves it out, and the index on movements (item_id, location_id) serves the sums.

### Each movement type is tied to its sign, reason and source

Section 6 fixes the direction of each action: use is negative, receive is positive and comes from a purchase order line, a move carries a transfer_id, a count movement carries its count, and an adjust needs a reason. The build test found two gaps here: an empty location had no defined opening balance, and nothing stopped a Found adjustment from removing stock. The spec now says only opening balances may be 0, Damaged and Lost only remove, and Found only adds, and each of these is a CHECK on movements. (This point comes from a gap the blueprint's build test found.)

### SKUs and location codes are stored in capitals

Section 5 says SKUs are unique regardless of case and stored in capitals because labels link to /i/<sku>, and location codes are stored in capitals too. The build test found that SKUs, codes and barcodes had no format at all, though SKUs and codes go into label URLs. A CHECK that the value equals upper(value) makes a plain UNIQUE constraint case-insensitive on both engines; the allowed characters are left to the app. (This point comes from a gap the blueprint's build test found.)

### One open or submitted count per location

Section 13 refuses a new count at a location while another there is open or submitted. The build test found that only an open count blocked a new one, so a submitted count awaiting review didn't, and approving both would correct the same stock twice. A partial unique index on counts (location_id) WHERE status IN ('open', 'submitted') enforces it in the database. (This point comes from a gap the blueprint's build test found.)

### A line can never be received past its ordered quantity

Section 7 refuses receiving more than is outstanding on a line ("Ordered 8, 0 received; you can receive up to 8.") and allows one line per item on an order. So po_lines has CHECK (qty_received BETWEEN 0 AND qty_ordered) and UNIQUE (po_id, item_id). Extra units that arrive anyway are recorded by an admin as a Found adjustment, not as a receipt.

### The outbox records when each email was written

Section 10 writes every email to the outbox before sending it and keeps it, with status logged when no provider is configured. The build test found the outbox record had no timestamp, so the Outbox screen couldn't show when an email was written. notifications has a written_at column for it. (This point comes from a gap the blueprint's build test found.)

### Cross-row rules stay in the app

No negative stock (R1), the low stock flag (R4), blind counts and the changed-during-count flag (R7), and archiving only empty items (R8) depend on sums across rows or on who is asking. Portable SQL cannot enforce them, so they are named in comments on the tables. R1 must check and write in one transaction so two people taking the last unit cannot both succeed.

## Example queries

### On order by item (R3)

```sql
SELECT i.sku, i.name, SUM(l.qty_ordered - l.qty_received) AS on_order FROM po_lines l JOIN purchase_orders p ON p.id = l.po_id JOIN items i ON i.id = l.item_id WHERE p.status IN ('sent', 'partially_received') GROUP BY i.sku, i.name ORDER BY i.sku;
```

On the seed data: 2 rows (GL-M, Nitrile gloves, 20 on order; VB-10, Vacuum bags, 8 on order).

### Purchase orders with totals (admins only)

```sql
SELECT p.number, s.name AS supplier, p.status, p.expected_date, SUM(l.qty_ordered * l.unit_cost_cents) AS total_cents FROM purchase_orders p JOIN suppliers s ON s.id = p.supplier_id LEFT JOIN po_lines l ON l.po_id = p.id GROUP BY p.number, s.name, p.status, p.expected_date ORDER BY p.number;
```

On the seed data: 2 rows (PO-0006, Metro Paper Co., received, 41700 cents; PO-0007, ProClean Wholesale, sent, 30960 cents).

### Overdue purchase orders for the daily job, on 2026-10-21

```sql
SELECT p.number, s.name AS supplier, p.expected_date, c.name AS created_by, c.email FROM purchase_orders p JOIN suppliers s ON s.id = p.supplier_id JOIN people c ON c.id = p.created_by WHERE p.status IN ('sent', 'partially_received') AND p.expected_date < '2026-10-21' ORDER BY p.number;
```

On the seed data: 1 row (PO-0007, ProClean Wholesale, expected 2026-10-20, Marco Silva, marco@brightside.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 is section 5 of the [Inventory Tracker blueprint](https://personalsoftwarefactory.com/blueprints/inventory-tracker), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/inventory-tracker/blueprint.md
