CRM database schema
A small-team CRM needs six tables: people, companies, contacts, deals, activities and digest_logs. The design is carried by contacts, where next_follow_up is required for every contact that is not archived, and by deals, where check constraints tie closed_on and lost_reason to the won and lost stages. Follow-up status (overdue, due today, upcoming) is never stored; it is computed from today's date at read time.
- 6 tables, 8 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
- 40 seed rows from the blueprint
- The blueprint's test build passed 27 of 27 acceptance checks
Simple CRM entity-relationship diagram
Relationships
- companies.owner_id references people.id: each companies row points to exactly one people row, and one people row can have many companies rows pointing to it.
- contacts.company_id references companies.id: each contacts row points to at most one companies row, and one companies row can have many contacts rows pointing to it.
- contacts.owner_id references people.id: each contacts row points to exactly one people row, and one people row can have many contacts rows pointing to it.
- deals.contact_id references contacts.id: each deals row points to exactly one contacts row, and one contacts row can have many deals rows pointing to it.
- deals.company_id references companies.id: each deals row points to at most one companies row, and one companies row can have many deals rows pointing to it.
- deals.owner_id references people.id: each deals row points to exactly one people row, and one people row can have many deals rows pointing to it.
- activities.contact_id references contacts.id: each activities row points to exactly one contacts row, and one contacts row can have many activities rows pointing to it.
- activities.deal_id references deals.id: each activities row points to at most one deals row, and one deals row can have many activities rows pointing to it.
- activities.by_id references people.id: each activities row points to exactly one people row, and one people row can have many activities rows pointing to it.
- digest_logs.person_id references people.id: each digest_logs row points to exactly one people row, and one people row can have many digest_logs rows pointing to it.
Simple CRM tables
The schema has 6 tables. Every column below is read from the SQL file itself. Also described as: simple CRM database design, sales pipeline database tables, contacts and deals data model.
| Table | Implements | What a row is |
|---|---|---|
people | Person | One row per person who uses the CRM, with their admin flag and whether they are active. |
companies | Company | One row per customer or prospect company, with its normalised domain and owner. |
contacts | Contact | One row per contact, with their company, owner, next follow-up date and archived flag. |
deals | Deal | One row per deal in the single pipeline, with its value in cents, stage and expected close date. |
activities | Activity | One row per call, email, meeting, note or app-written system entry on a contact's timeline. |
digest_logs | DigestLog | One row per daily digest email per person and day, written before sending. |
people
One row per person who uses the CRM.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
email | TEXT | no | unique ignoring case (index below); store it trimmed | ||
is_admin | BOOLEAN | no | FALSE | ||
active | BOOLEAN | no | TRUE |
- Unique index
people_email_idxon(lower(email)). Case-insensitive uniqueness (R12, R13, section 5). - App rule (R19): only admins add or deactivate people, an admin cannot deactivate themselves, and deactivating reassigns their records.
companies
One row per company (an organization the team sells to).
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | unique ignoring case (index below); store it trimmedCheck: length(name) BETWEEN 1 AND 120 | ||
domain | TEXT | yes | Unique | normalised, for example acme.example; empty when unknownCheck: domain = lower(domain) AND domain NOT LIKE '%/%' AND domain NOT LIKE 'www.%' | |
owner_id | TEXT | no | References people.id | ||
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP |
- Unique index
companies_name_idxon(lower(name)) - App rule (R9): owner_id must point at an active person.
- App rule (R13): a contact form or import row naming an unknown company creates it, owned by the contact's owner.
contacts
One row per contact (a person at a customer or prospect).
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | Check: length(name) BETWEEN 1 AND 120 | ||
email | TEXT | yes | optional; unique ignoring case across all contacts, archived included (index below) | ||
phone | TEXT | yes | free text | ||
title | TEXT | yes | job title | ||
company_id | TEXT | yes | References companies.id | ||
owner_id | TEXT | no | References people.id | ||
next_follow_up | DATE | yes | required while the contact is not archived (R1); overdue, due today and upcoming are computed from it, never stored | ||
archived | BOOLEAN | no | FALSE | ||
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP | ||
last_activity_on | DATE | yes | derived: latest happened_on of this contact's call, email, meeting and note activities; empty if none. Kept in sync by the app, or drop it and compute it |
- Table check:
archived = TRUE OR next_follow_up IS NOT NULL - Unique index
contacts_email_idxon(lower(email)) - Index
contacts_owner_follow_up_idxon(owner_id, next_follow_up). The contact list and Today screen: follow-up order per owner (R3). - Index
contacts_company_idxon(company_id) - App rule (R4): a follow-up date entered or changed by hand must be today or later.
- App rule (R5): logging a call, email, meeting or note sets next_follow_up to tomorrow or later in the same transaction.
- App rule (R9): owner_id must point at an active person.
- App rule (R14): archiving is refused while the contact has an open deal; unarchiving sets next_follow_up to today if it is empty or past.
deals
One row per deal in the single pipeline.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
title | TEXT | no | Check: length(title) BETWEEN 1 AND 120 | ||
contact_id | TEXT | no | References contacts.id | ||
company_id | TEXT | yes | References companies.id | copied from the contact's company whenever the deal's contact is set; not updated when the contact later changes company | |
owner_id | TEXT | no | References people.id | ||
value_cents | INTEGER | no | integer cents in the team currency (TEAM_CURRENCY)Check: value_cents >= 0 | ||
stage | TEXT | no | probabilities 10, 30, 60, 80, 100 and 0 percent (section 7)Check: stage IN ('lead', 'demo', 'proposal', 'negotiation', 'won', 'lost') | ||
expected_close | DATE | no | |||
closed_on | DATE | yes | set exactly when the stage is won or lost | ||
lost_reason | TEXT | yes | set exactly when the stage is lost, at least 3 characters | ||
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP |
- Table check:
(stage IN ('won', 'lost') AND closed_on IS NOT NULL) OR (stage NOT IN ('won', 'lost') AND closed_on IS NULL) - Table check:
(stage = 'lost' AND lost_reason IS NOT NULL AND length(lost_reason) >= 3) OR (stage <> 'lost' AND lost_reason IS NULL) - Index
deals_contact_idxon(contact_id) - Index
deals_stage_idxon(stage, expected_close) - App rule (R15): a deal is created in an open stage, for a contact who is not archived; a new or changed expected_close on an open deal must be today or later.
- App rule (R17): won and lost set closed_on to today; reopening clears closed_on and lost_reason. Every stage change writes a system activity.
- App rule (R9): while the deal is open, owner_id must point at an active person. Closed deals keep their original owner (R19).
activities
One row per entry on a contact's timeline.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
contact_id | TEXT | no | References contacts.id | ||
deal_id | TEXT | yes | References deals.id | set when the activity is about a deal, including every stage change | |
type | TEXT | no | system rows are written by the app onlyCheck: type IN ('call', 'email', 'meeting', 'note', 'system') | ||
happened_on | DATE | no | |||
note | TEXT | yes | Check: note IS NULL OR length(note) <= 2000 | ||
by_id | TEXT | no | References people.id | the person who logged it, or whose action wrote the system row | |
created_at | TIMESTAMP | no | CURRENT_TIMESTAMP |
- Table check:
type NOT IN ('note', 'system') OR note IS NOT NULL - Table check:
type <> 'note' OR length(note) >= 3 - Index
activities_contact_idxon(contact_id, happened_on) - App rule (R6): happened_on must be today or earlier.
- App rule (R7): archived contacts cannot get new activities.
- App rule (R8): the author or an admin can delete a call, email, meeting or note; system activities are never deleted, and deleting never changes the follow-up date.
- App rule (R11): changing a contact's owner or follow-up date, including by logging an activity, writes a system activity.
digest_logs
One row per daily digest email, written before sending so the job never sends twice.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
person_id | TEXT | no | References people.id | ||
date | DATE | no | the day the digest covers, in the team time zone | ||
overdue_count | INTEGER | no | Check: overdue_count >= 0 | ||
due_today_count | INTEGER | no | Check: due_today_count >= 0 | ||
deals_count | INTEGER | no | open deals past close date or closing within 7 daysCheck: deals_count >= 0 | ||
sent_at | TIMESTAMP | yes | empty until the email is sent | ||
error | TEXT | yes | the last send error, if any |
- Unique together:
person_id, date - App rule (section 10): a failed send keeps its row with the error and is retried on the next run.
The SQL
The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download simple-crm.sql or copy it from here.
-- Simple CRM database schema
-- Source: https://personalsoftwarefactory.com/blueprints/simple-crm (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 uses the CRM.
-- App rule (R19): only admins add or deactivate people, an admin cannot deactivate themselves, and deactivating reassigns their records.
CREATE TABLE people (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL, -- unique ignoring case (index below); store it trimmed
is_admin BOOLEAN NOT NULL DEFAULT FALSE,
active BOOLEAN NOT NULL DEFAULT TRUE
);
-- One row per company (an organization the team sells to).
-- App rule (R9): owner_id must point at an active person.
-- App rule (R13): a contact form or import row naming an unknown company creates it, owned by the contact's owner.
CREATE TABLE companies (
id TEXT PRIMARY KEY,
name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 120), -- unique ignoring case (index below); store it trimmed
domain TEXT UNIQUE CHECK (domain = lower(domain) AND domain NOT LIKE '%/%' AND domain NOT LIKE 'www.%'), -- normalised, for example acme.example; empty when unknown
owner_id TEXT NOT NULL REFERENCES people(id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- One row per contact (a person at a customer or prospect).
-- App rule (R4): a follow-up date entered or changed by hand must be today or later.
-- App rule (R5): logging a call, email, meeting or note sets next_follow_up to tomorrow or later in the same transaction.
-- App rule (R9): owner_id must point at an active person.
-- App rule (R14): archiving is refused while the contact has an open deal; unarchiving sets next_follow_up to today if it is empty or past.
CREATE TABLE contacts (
id TEXT PRIMARY KEY,
name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 120),
email TEXT, -- optional; unique ignoring case across all contacts, archived included (index below)
phone TEXT, -- free text
title TEXT, -- job title
company_id TEXT REFERENCES companies(id),
owner_id TEXT NOT NULL REFERENCES people(id),
next_follow_up DATE, -- required while the contact is not archived (R1); overdue, due today and upcoming are computed from it, never stored
archived BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_activity_on DATE, -- derived: latest happened_on of this contact's call, email, meeting and note activities; empty if none. Kept in sync by the app, or drop it and compute it
CHECK (archived = TRUE OR next_follow_up IS NOT NULL)
);
-- One row per deal in the single pipeline.
-- App rule (R15): a deal is created in an open stage, for a contact who is not archived; a new or changed expected_close on an open deal must be today or later.
-- App rule (R17): won and lost set closed_on to today; reopening clears closed_on and lost_reason. Every stage change writes a system activity.
-- App rule (R9): while the deal is open, owner_id must point at an active person. Closed deals keep their original owner (R19).
CREATE TABLE deals (
id TEXT PRIMARY KEY,
title TEXT NOT NULL CHECK (length(title) BETWEEN 1 AND 120),
contact_id TEXT NOT NULL REFERENCES contacts(id),
company_id TEXT REFERENCES companies(id), -- copied from the contact's company whenever the deal's contact is set; not updated when the contact later changes company
owner_id TEXT NOT NULL REFERENCES people(id),
value_cents INTEGER NOT NULL CHECK (value_cents >= 0), -- integer cents in the team currency (TEAM_CURRENCY)
stage TEXT NOT NULL CHECK (stage IN ('lead', 'demo', 'proposal', 'negotiation', 'won', 'lost')), -- probabilities 10, 30, 60, 80, 100 and 0 percent (section 7)
expected_close DATE NOT NULL,
closed_on DATE, -- set exactly when the stage is won or lost
lost_reason TEXT, -- set exactly when the stage is lost, at least 3 characters
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHECK ((stage IN ('won', 'lost') AND closed_on IS NOT NULL) OR (stage NOT IN ('won', 'lost') AND closed_on IS NULL)),
CHECK ((stage = 'lost' AND lost_reason IS NOT NULL AND length(lost_reason) >= 3) OR (stage <> 'lost' AND lost_reason IS NULL))
);
-- One row per entry on a contact's timeline.
-- App rule (R6): happened_on must be today or earlier.
-- App rule (R7): archived contacts cannot get new activities.
-- App rule (R8): the author or an admin can delete a call, email, meeting or note; system activities are never deleted, and deleting never changes the follow-up date.
-- App rule (R11): changing a contact's owner or follow-up date, including by logging an activity, writes a system activity.
CREATE TABLE activities (
id TEXT PRIMARY KEY,
contact_id TEXT NOT NULL REFERENCES contacts(id),
deal_id TEXT REFERENCES deals(id), -- set when the activity is about a deal, including every stage change
type TEXT NOT NULL CHECK (type IN ('call', 'email', 'meeting', 'note', 'system')), -- system rows are written by the app only
happened_on DATE NOT NULL,
note TEXT CHECK (note IS NULL OR length(note) <= 2000),
by_id TEXT NOT NULL REFERENCES people(id), -- the person who logged it, or whose action wrote the system row
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHECK (type NOT IN ('note', 'system') OR note IS NOT NULL),
CHECK (type <> 'note' OR length(note) >= 3)
);
-- One row per daily digest email, written before sending so the job never sends twice.
-- App rule (section 10): a failed send keeps its row with the error and is retried on the next run.
CREATE TABLE digest_logs (
id TEXT PRIMARY KEY,
person_id TEXT NOT NULL REFERENCES people(id),
date DATE NOT NULL, -- the day the digest covers, in the team time zone
overdue_count INTEGER NOT NULL CHECK (overdue_count >= 0),
due_today_count INTEGER NOT NULL CHECK (due_today_count >= 0),
deals_count INTEGER NOT NULL CHECK (deals_count >= 0), -- open deals past close date or closing within 7 days
sent_at TIMESTAMP, -- empty until the email is sent
error TEXT, -- the last send error, if any
UNIQUE (person_id, date)
);
-- Case-insensitive uniqueness (R12, R13, section 5).
CREATE UNIQUE INDEX people_email_idx ON people (lower(email));
CREATE UNIQUE INDEX companies_name_idx ON companies (lower(name));
CREATE UNIQUE INDEX contacts_email_idx ON contacts (lower(email));
-- The contact list and Today screen: follow-up order per owner (R3).
CREATE INDEX contacts_owner_follow_up_idx ON contacts (owner_id, next_follow_up);
CREATE INDEX contacts_company_idx ON contacts (company_id);
CREATE INDEX deals_contact_idx ON deals (contact_id);
CREATE INDEX deals_stage_idx ON deals (stage, expected_close);
CREATE INDEX activities_contact_idx ON activities (contact_id, happened_on);Seed data
Loads the spec's seed data in full for people (7), companies (7), contacts (14, including archived Ruth Adams) and deals (12: 9 open, 2 won, 1 lost), with T fixed at 2026-10-14. Activities and digest logs are empty: the spec only asks for one to three past activities per contact without saying what they are, so none are invented, and last_activity_on is empty for every contact. Phone numbers are empty because the spec gives none.
Download simple-crm-seed.sql (40 rows). It loads with foreign keys enforced on both engines.
Load it
SQLite, into a new file:
curl -s https://personalsoftwarefactory.com/schemas/simple-crm.sql https://personalsoftwarefactory.com/schemas/simple-crm-seed.sql | sqlite3 simple_crm.dbPostgreSQL, into a new database:
createdb simple_crm && curl -s https://personalsoftwarefactory.com/schemas/simple-crm.sql https://personalsoftwarefactory.com/schemas/simple-crm-seed.sql | psql -v ON_ERROR_STOP=1 simple_crmSQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.
Design decisions
next_follow_up is required unless the contact is archived
From the build test · field report
Rule R1 says every contact that is not archived has exactly one next follow-up date that can be moved but never cleared, so a table check refuses an active contact without one. Archived contacts may have none, like Ruth Adams in the seed. The build test found that R14 did not say what unarchiving does when no date is stored, as for Ruth Adams (gap 3); the spec now sets it to today, which the app does when it flips archived back.
Follow-up status is computed, never stored
Rule R2 defines overdue, due today and upcoming by comparing next_follow_up with today in the team time zone, and section 10 says overdue status is always computed, never stored. A stored status column would be wrong the morning after it was written. The queries below pass today's date in as a literal; your app passes the team's today.
Closing fields are tied to the stage by check constraints
Rule R17 says won and lost set closed_on, lost requires a reason of at least 3 characters, and reopening clears both. Two table checks make closed_on present exactly on won and lost deals, and lost_reason present exactly on lost ones. Without them a reopened deal can keep an old closed date and still count in Won this month.
Stage probabilities live in the query, not in a table
Section 7 fixes one pipeline with six stages and fixed probabilities, so stage is a checked text column and the weighted total maps stage to percent with CASE. Rule R18 rounds the weighted value to the nearest cent per deal before summing, which the board query does in integer cents. If you add custom stages (a variation in section 14), move them into a stages table.
Uniqueness ignores case through lower() indexes
Section 5 makes person email, company name and contact email unique ignoring case, and R12 counts archived contacts too. Unique indexes on lower(email) and lower(name) enforce that on both engines while still allowing many contacts with no email. Trim values before saving, since R12 and R13 also compare trimmed.
A deal copies the contact's company
Section 5 and the lifecycle table in section 7 copy company_id from the contact whenever a deal's contact is set. Section 11 relies on this: when a contact moves to a new company, their existing deals keep the old one. Joining deals to companies through contacts instead would rewrite deal history.
The digest log is unique per person and day
Section 10 writes a DigestLog row before sending and sends nothing if a row already exists for that person and date, so the job is safe to run twice. A unique constraint on (person_id, date) makes a second concurrent run fail instead of sending a duplicate email. A failed send keeps its row with the error for the retry.
Example queries
The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.
Contact list in follow-up order (Everyone)
SELECT c.name, co.name AS company, o.name AS owner, c.next_follow_up, CASE WHEN c.next_follow_up < '2026-10-14' THEN 'overdue' WHEN c.next_follow_up = '2026-10-14' THEN 'due today' ELSE 'upcoming' END AS follow_up_status FROM contacts c LEFT JOIN companies co ON co.id = c.company_id JOIN people o ON o.id = c.owner_id WHERE c.archived = FALSE ORDER BY c.next_follow_up, c.name;On the seed data with today 2026-10-14: 13 rows, Grace Kim (overdue, 2026-10-08) first, Kim Olsen before Nina Patel on the same day, Felix Wong last. Ruth Adams is archived and not listed.
Pipeline board totals per open stage
SELECT d.stage, COUNT(*) AS deals, SUM(d.value_cents) AS total_cents, SUM((d.value_cents * CASE d.stage WHEN 'lead' THEN 10 WHEN 'demo' THEN 30 WHEN 'proposal' THEN 60 WHEN 'negotiation' THEN 80 END + 50) / 100) AS weighted_cents FROM deals d WHERE d.stage IN ('lead', 'demo', 'proposal', 'negotiation') GROUP BY d.stage ORDER BY CASE d.stage WHEN 'lead' THEN 1 WHEN 'demo' THEN 2 WHEN 'proposal' THEN 3 ELSE 4 END;On the seed data: 4 rows, matching section 7. Proposal has 2 deals, 2780000 cents total and 1668000 weighted; the four weighted values add up to 6122000 cents ($61,220).
Daily digest counts per person
SELECT p.name, (SELECT COUNT(*) FROM contacts c WHERE c.owner_id = p.id AND c.archived = FALSE AND c.next_follow_up < '2026-10-14') AS overdue, (SELECT COUNT(*) FROM contacts c WHERE c.owner_id = p.id AND c.archived = FALSE AND c.next_follow_up = '2026-10-14') AS due_today, (SELECT COUNT(*) FROM deals d WHERE d.owner_id = p.id AND d.stage IN ('lead', 'demo', 'proposal', 'negotiation') AND d.expected_close <= '2026-10-21') AS deals_to_watch FROM people p WHERE p.active = TRUE ORDER BY p.name;On the seed data with today 2026-10-14: 7 rows, one per active person. Sam Okafor has 2 overdue, 0 due today and 1 deal, the spec's own example; Ana Lima, Ben Cho and Jo Park have all zeros, so the job sends them nothing.
Deals to watch: past close date or closing within 7 days
SELECT d.title, o.name AS owner, d.expected_close, CASE WHEN d.expected_close < '2026-10-14' THEN 'Past close date' ELSE 'Closing soon' END AS flag FROM deals d JOIN people o ON o.id = d.owner_id WHERE d.stage IN ('lead', 'demo', 'proposal', 'negotiation') AND d.expected_close <= '2026-10-21' ORDER BY d.expected_close, d.title;On the seed data with today 2026-10-14: 3 rows. Acme pilot is Past close date (2026-10-12); Harbor renewal and Olsen & Co rollout are closing soon.
Companies with active contacts, open deal value and earliest follow-up
SELECT co.name, co.domain, o.name AS owner, (SELECT COUNT(*) FROM contacts c WHERE c.company_id = co.id AND c.archived = FALSE) AS active_contacts, (SELECT COALESCE(SUM(d.value_cents), 0) FROM deals d WHERE d.company_id = co.id AND d.stage IN ('lead', 'demo', 'proposal', 'negotiation')) AS open_value_cents, (SELECT MIN(c.next_follow_up) FROM contacts c WHERE c.company_id = co.id AND c.archived = FALSE) AS earliest_follow_up FROM companies co JOIN people o ON o.id = co.owner_id ORDER BY co.name;On the seed data: 7 rows. Olsen & Co has 2 active contacts, 3200000 cents open and earliest follow-up 2026-10-14; Kestrel has 1 active contact because Ruth Adams is archived.
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 Simple CRM blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 27 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 27 of 27 acceptance checks in a browser.
- Read the Simple CRM 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.