Knowledge base database schema

Fifteen tables. The design is carried by cards, which hold one question, one owner and a review interval, plus card_versions for every publish and flags for reports that a card is wrong. Whether a card is Verified is never stored: it is computed from the owner, open flags, the edit state and the review date, so it can't go stale. Alternate questions and tags are child tables with a position column whose range caps them at 5 and 8 per card.

  • 15 tables, 5 indexes
  • Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
  • 126 seed rows from the blueprint
  • The blueprint's test build passed 31 of 31 acceptance checks

Copy the SQLDownload SQLDownload seed data

By Sam Claassen · From the Internal Knowledge Base blueprint · MIT licensed · Markdown version

Internal knowledge base entity-relationship diagram

settingsidPK+ 3 more columnsteamsidPK+ 1 more columnpeopleidPKteam_id→ teams+ 6 more columnscollectionsidPK+ 3 more columnscollection_teamscollection_idPK → collectionsteam_idPK → teamscollection_editorscollection_idPK → collectionsperson_idPK → peoplecardsidPKcollection_id→ collectionsowner_id→ peopleverified_by→ peopleneeds_verification_by→ peoplecreated_by→ peopleupdated_by→ peoplearchived_by→ people+ 15 more columnscard_questionscard_idPK → cardspositionPK+ 1 more columncard_tagscard_idPK → cardspositionPK+ 1 more columncard_versionsidPKcard_id→ cardsauthor_id→ people+ 7 more columnsflagsidPKcard_id→ cardsperson_id→ peopleresolved_by→ people+ 5 more columnsfollowscard_idPK → cardsperson_idPK → people+ 1 more columncard_eventsidPKcard_id→ cardsactor_id→ people+ 3 more columnssearch_logsidPK+ 3 more columnsnotificationsidPKperson_id→ peoplecard_id→ cards+ 9 more columnssettingsidPK+ 3 more columnsteamsidPK+ 1 more columnpeopleidPKteam_id→ teams+ 6 more columnscollectionsidPK+ 3 more columnscollection_teamscollection_idPK → collectionsteam_idPK → teamscollection_editorscollection_idPK → collectionsperson_idPK → peoplecardsidPKcollection_id→ collectionsowner_id→ peopleverified_by→ peopleneeds_verification_by→ peoplecreated_by→ peopleupdated_by→ peoplearchived_by→ people+ 15 more columnscard_questionscard_idPK → cardspositionPK+ 1 more columncard_tagscard_idPK → cardspositionPK+ 1 more columncard_versionsidPKcard_id→ cardsauthor_id→ people+ 7 more columnsflagsidPKcard_id→ cardsperson_id→ peopleresolved_by→ people+ 5 more columnsfollowscard_idPK → cardsperson_idPK → people+ 1 more columncard_eventsidPKcard_id→ cardsactor_id→ people+ 3 more columnssearch_logsidPK+ 3 more columnsnotificationsidPKperson_id→ peoplecard_id→ cards+ 9 more columns
Each box is a table with its key columns. A line runs from a table up to a table it references, with the forked end on the side that has many rows; a dashed line is an optional reference.

Relationships

  • people.team_id references teams.id: each people row points to exactly one teams row, and one teams row can have many people rows pointing to it.
  • collection_teams.collection_id references collections.id: each collection_teams row points to exactly one collections row, and one collections row can have many collection_teams rows pointing to it. On delete: cascade.
  • collection_teams.team_id references teams.id: each collection_teams row points to exactly one teams row, and one teams row can have many collection_teams rows pointing to it.
  • collection_editors.collection_id references collections.id: each collection_editors row points to exactly one collections row, and one collections row can have many collection_editors rows pointing to it. On delete: cascade.
  • collection_editors.person_id references people.id: each collection_editors row points to exactly one people row, and one people row can have many collection_editors rows pointing to it.
  • cards.collection_id references collections.id: each cards row points to exactly one collections row, and one collections row can have many cards rows pointing to it.
  • cards.owner_id references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
  • cards.verified_by references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
  • cards.needs_verification_by references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
  • cards.created_by references people.id: each cards row points to exactly one people row, and one people row can have many cards rows pointing to it.
  • cards.updated_by references people.id: each cards row points to exactly one people row, and one people row can have many cards rows pointing to it.
  • cards.archived_by references people.id: each cards row points to at most one people row, and one people row can have many cards rows pointing to it.
  • card_questions.card_id references cards.id: each card_questions row points to exactly one cards row, and one cards row can have many card_questions rows pointing to it. On delete: cascade.
  • card_tags.card_id references cards.id: each card_tags row points to exactly one cards row, and one cards row can have many card_tags rows pointing to it. On delete: cascade.
  • card_versions.card_id references cards.id: each card_versions row points to exactly one cards row, and one cards row can have many card_versions rows pointing to it.
  • card_versions.author_id references people.id: each card_versions row points to exactly one people row, and one people row can have many card_versions rows pointing to it.
  • flags.card_id references cards.id: each flags row points to exactly one cards row, and one cards row can have many flags rows pointing to it.
  • flags.person_id references people.id: each flags row points to exactly one people row, and one people row can have many flags rows pointing to it.
  • flags.resolved_by references people.id: each flags row points to at most one people row, and one people row can have many flags rows pointing to it.
  • follows.card_id references cards.id: each follows row points to exactly one cards row, and one cards row can have many follows rows pointing to it. On delete: cascade.
  • follows.person_id references people.id: each follows row points to exactly one people row, and one people row can have many follows rows pointing to it.
  • card_events.card_id references cards.id: each card_events row points to exactly one cards row, and one cards row can have many card_events rows pointing to it. On delete: cascade.
  • card_events.actor_id references people.id: each card_events row points to at most one people row, and one people row can have many card_events rows pointing to it.
  • notifications.person_id references people.id: each notifications row points to exactly one people row, and one people row can have many notifications rows pointing to it.
  • notifications.card_id references cards.id: each notifications row points to exactly one cards row, and one cards row can have many notifications rows pointing to it.

Internal knowledge base tables

The schema has 15 tables. Every column below is read from the SQL file itself. Also described as: internal wiki database design, knowledge management system database tables, FAQ database schema.

TableImplementsWhat a row is
settingsSettingsThe one row holding the company name, the team time zone and when the daily job last ran.
teamsTeamA team; each person belongs to one, and collections can be limited to teams.
peoplePersonA person who can sign in, with their team, admin flag, active flag and Slack user ID.
collectionsCollectionA group of cards visible to everyone or to chosen teams.
collection_teamsCollectionTeamA team that can see a collection whose visibility is teams.
collection_editorsCollectionEditorA person who can create and edit cards in a collection.
cardsCardOne question and its answer, with its owner, review interval, last verification and status.
card_questionschild table of CardOne alternate phrasing of a card's question, in display order.
card_tagschild table of CardOne tag on a card, in display order.
card_versionsCardVersionA published version of a card, written on every publish and never changed.
flagsFlagA report that a card is wrong, open until verifying, archiving or withdrawing resolves it.
followsFollowA person following a card to hear about new versions.
card_eventsCardEventOne action on a card, append-only, shown as the card's Activity.
search_logsSearchLogOne submitted search: the terms, the number of results and the time, never who searched.
notificationsNotificationOne email in the outbox with its kind, recipient, status and send attempts.

settings

The single settings row. id is fixed at 1 so the table can never hold a second row.

ColumnTypeNullDefaultKeyNotes
idINTEGERnoPrimary keyCheck: id = 1
company_nameTEXTnoCheck: length(company_name) BETWEEN 1 AND 60
time_zoneTEXTno'America/New_York'IANA name; "today" and every date shown use this zone
last_daily_run_atTIMESTAMPyeswritten only by the scheduled job

teams

A team. Each person belongs to exactly one.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
nameTEXTnoCheck: length(name) BETWEEN 1 AND 40
  • Unique index teams_name_unique on (lower(name))

people

A person who can sign in. People are deactivated, never deleted.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
nameTEXTnoCheck: length(name) BETWEEN 1 AND 80
emailTEXTnoUniquestored lowercaseCheck: email = lower(email)
team_idTEXTnoReferences teams.id
is_adminBOOLEANnoFALSE
activeBOOLEANnoTRUE
slack_user_idTEXTyesUniqueset by Link Slack accounts or by hand
slack_lookup_errorTEXTyesthe Slack API error from the last failed lookup
  • App rule (R15): at least one active admin must always exist.

collections

A collection of cards, visible to everyone or to chosen teams.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
nameTEXTnoCheck: length(name) BETWEEN 1 AND 60
descriptionTEXTyesCheck: length(description) <= 300
visibilityTEXTnoCheck: visibility IN ('everyone', 'teams')
  • Unique index collections_name_unique on (lower(name))
  • App rule (R3): every editor must be active and able to see the collection.

collection_teams

A team that can see a collection whose visibility is teams.

ColumnTypeNullDefaultKeyNotes
collection_idTEXTnoPrimary key (part); References collections.id, on delete cascade
team_idTEXTnoPrimary key (part); References teams.ida team used by a collection cannot be deleted (R15)
  • App rule: a collection with visibility teams needs at least one row here.

collection_editors

A person who can edit the cards in a collection. Admins edit every collection without a row here.

ColumnTypeNullDefaultKeyNotes
collection_idTEXTnoPrimary key (part); References collections.id, on delete cascade
person_idTEXTnoPrimary key (part); References people.id

cards

A card: one question and its answer. Trust (Verified or Unverified, with a reason) is computed, never stored (R12).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
numberINTEGERnoUniqueshown as KB-101Check: number >= 101
collection_idTEXTnoReferences collections.ida collection with cards cannot be deleted
statusTEXTno'draft'Check: status IN ('draft', 'published', 'archived')
titleTEXTnowritten as the question people askCheck: length(title) BETWEEN 5 AND 120
bodyTEXTno''MarkdownCheck: length(body) <= 5000
owner_idTEXTyesReferences people.idrequired to publish
review_interval_daysINTEGERyes90NULL means does not expireCheck: review_interval_days IN (30, 90, 180, 365)
verified_atTIMESTAMPyes
verified_byTEXTyesReferences people.id
verified_untilDATEyesdate of verified_at in the team zone plus review_interval_days; NULL when it does not expire
needs_verificationTEXTyesNULL means emptyCheck: needs_verification IN ('not_verified', 'edited', 'restored')
needs_verification_byTEXTyesReferences people.id
needs_verification_atTIMESTAMPyes
versionINTEGERno00 until first published, then the current version number
created_byTEXTnoReferences people.id
created_atTIMESTAMPno
updated_byTEXTnoReferences people.idthe author of the current version (the last save for drafts)
updated_atTIMESTAMPno
archived_byTEXTyesReferences people.id
archived_atTIMESTAMPyes
reminder_sent_forDATEyesthe verified_until value the review_due notice was sent for
overdue_sent_forDATEyesthe verified_until value the review_overdue notice was sent for
  • Table check: status = 'draft' OR owner_id IS NOT NULL
  • Table check: status = 'draft' OR length(body) >= 1
  • Table check: (status = 'draft' AND version = 0) OR (status <> 'draft' AND version >= 1)
  • Table check: verified_at IS NOT NULL OR (verified_by IS NULL AND verified_until IS NULL)
  • App rule (R5): number is assigned in order from 101 and never reused, even after a draft is deleted.
  • App rule (R4, R10): the owner must be an active editor of the collection or an admin when set.
  • App rule (section 7): status changes follow the lifecycle table; only drafts can be deleted.

card_questions

An alternate phrasing of the question on a card (0 to 5 per card, in display order).

ColumnTypeNullDefaultKeyNotes
card_idTEXTnoPrimary key (part); References cards.id, on delete cascade
positionINTEGERnoPrimary key (part)the 1 to 5 range caps a card at 5 questionsCheck: position BETWEEN 1 AND 5
questionTEXTnoCheck: length(question) BETWEEN 3 AND 120

card_tags

A tag on a card (0 to 8 per card, in display order).

ColumnTypeNullDefaultKeyNotes
card_idTEXTnoPrimary key (part); References cards.id, on delete cascade
positionINTEGERnoPrimary key (part)the 1 to 8 range caps a card at 8 tagsCheck: position BETWEEN 1 AND 8
tagTEXTnoCheck: length(tag) BETWEEN 2 AND 30 AND tag = lower(tag)
  • Unique together: card_id, tag
  • App rule (R4): tags use only lowercase letters, digits and hyphens.

card_versions

A published version of a card, written on every publish and never changed or deleted (R6).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
card_idTEXTnoReferences cards.id
numberINTEGERnoCheck: number >= 1
titleTEXTno
bodyTEXTno
questionsTEXTno''the alternate questions as published, one per line, in order
tagsTEXTno''the tags as published, one per line, in order
change_noteTEXTno''empty for version 1, 3 to 200 characters after that
author_idTEXTnoReferences people.id
published_atTIMESTAMPno
  • Unique together: card_id, number
  • Table check: (number = 1 AND change_note = '') OR (number > 1 AND length(change_note) BETWEEN 3 AND 200)

flags

A flag saying a card is wrong or out of date. An open flag makes the card Unverified.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
card_idTEXTnoReferences cards.id
person_idTEXTnoReferences people.idwho flagged
reasonTEXTnoCheck: length(reason) BETWEEN 5 AND 500
created_atTIMESTAMPno
statusTEXTno'open'Check: status IN ('open', 'resolved')
resolved_atTIMESTAMPyes
resolved_byTEXTyesReferences people.id
resolutionTEXTyesCheck: resolution IN ('verified', 'archived', 'withdrawn')
  • Table check: (status = 'open' AND resolved_at IS NULL AND resolution IS NULL) OR (status = 'resolved' AND resolved_at IS NOT NULL AND resolution IS NOT NULL)
  • Unique index flags_one_open_per_person on (card_id, person_id) where status = 'open'
  • App rule (R9): the owner of the card cannot flag it; only the person who flagged can withdraw it.

follows

A person following a card, told when a new version is published with "Notify followers".

ColumnTypeNullDefaultKeyNotes
card_idTEXTnoPrimary key (part); References cards.id, on delete cascade
person_idTEXTnoPrimary key (part); References people.id
created_atTIMESTAMPno

card_events

One action on a card. Append-only; rows go only when a draft is deleted with its card (R19).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
card_idTEXTnoReferences cards.id, on delete cascade
actor_idTEXTyesReferences people.idNULL for the system
created_atTIMESTAMPno
typeTEXTnoCheck: type IN ('created', 'published', 'updated', 'verified', 'flagged', 'flag_withdrawn', 'owner_changed', 'archived', 'restored')
dataTEXTno'{}'a JSON object stored as text: version number, old and new owner, flags resolved

search_logs

One submitted search. Never stores who searched (R14). Rows older than 90 days are deleted by the daily job.

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
queryTEXTnothe search terms joined by single spacesCheck: length(query) BETWEEN 1 AND 200
result_countINTEGERnoresults shown, at most 20Check: result_count BETWEEN 0 AND 20
created_atTIMESTAMPno
  • Index search_logs_created_at on (created_at)

notifications

One email in the outbox. Written in the same transaction as its action and sent after it commits (R17).

ColumnTypeNullDefaultKeyNotes
idTEXTnoPrimary key
kindTEXTnoCheck: kind IN ('verify_request', 'card_updated', 'flagged', 'flag_resolved', 'review_due', 'review_overdue')
person_idTEXTnoReferences people.idthe recipient
to_emailTEXTno
card_idTEXTnoReferences cards.id
subjectTEXTno
bodyTEXTnoplain text
statusTEXTno'queued'Check: status IN ('queued', 'sent', 'logged', 'failed')
attemptsINTEGERno0each send attempt adds 1; logging is not an attempt; retried until 5Check: attempts >= 0
last_errorTEXTyes
created_atTIMESTAMPno
sent_atTIMESTAMPyes
  • Index notifications_status on (status)

The SQL

The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download knowledge-base.sql or copy it from here.

knowledge-base.sql
-- Internal Knowledge Base database schema
-- Source: https://personalsoftwarefactory.com/blueprints/knowledge-base (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),
  company_name TEXT NOT NULL CHECK (length(company_name) BETWEEN 1 AND 60),
  time_zone TEXT NOT NULL DEFAULT 'America/New_York', -- IANA name; "today" and every date shown use this zone
  last_daily_run_at TIMESTAMP -- written only by the scheduled job
);

-- A team. Each person belongs to exactly one.
CREATE TABLE teams (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 40)
);
CREATE UNIQUE INDEX teams_name_unique ON teams (lower(name)); -- unique ignoring case

-- A person who can sign in. People are deactivated, never deleted.
-- App rule (R15): at least one active admin must always exist.
CREATE TABLE people (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 80),
  email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase
  team_id TEXT NOT NULL REFERENCES teams(id),
  is_admin BOOLEAN NOT NULL DEFAULT FALSE,
  active BOOLEAN NOT NULL DEFAULT TRUE,
  slack_user_id TEXT UNIQUE, -- set by Link Slack accounts or by hand
  slack_lookup_error TEXT -- the Slack API error from the last failed lookup
);

-- A collection of cards, visible to everyone or to chosen teams.
-- App rule (R3): every editor must be active and able to see the collection.
CREATE TABLE collections (
  id TEXT PRIMARY KEY,
  name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 60),
  description TEXT CHECK (length(description) <= 300),
  visibility TEXT NOT NULL CHECK (visibility IN ('everyone', 'teams'))
);
CREATE UNIQUE INDEX collections_name_unique ON collections (lower(name)); -- unique ignoring case

-- A team that can see a collection whose visibility is teams.
-- App rule: a collection with visibility teams needs at least one row here.
CREATE TABLE collection_teams (
  collection_id TEXT NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
  team_id TEXT NOT NULL REFERENCES teams(id), -- a team used by a collection cannot be deleted (R15)
  PRIMARY KEY (collection_id, team_id)
);

-- A person who can edit the cards in a collection. Admins edit every collection without a row here.
CREATE TABLE collection_editors (
  collection_id TEXT NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
  person_id TEXT NOT NULL REFERENCES people(id),
  PRIMARY KEY (collection_id, person_id)
);

-- A card: one question and its answer. Trust (Verified or Unverified, with a reason) is computed, never stored (R12).
-- App rule (R5): number is assigned in order from 101 and never reused, even after a draft is deleted.
-- App rule (R4, R10): the owner must be an active editor of the collection or an admin when set.
-- App rule (section 7): status changes follow the lifecycle table; only drafts can be deleted.
CREATE TABLE cards (
  id TEXT PRIMARY KEY,
  number INTEGER NOT NULL UNIQUE CHECK (number >= 101), -- shown as KB-101
  collection_id TEXT NOT NULL REFERENCES collections(id), -- a collection with cards cannot be deleted
  status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'published', 'archived')),
  title TEXT NOT NULL CHECK (length(title) BETWEEN 5 AND 120), -- written as the question people ask
  body TEXT NOT NULL DEFAULT '' CHECK (length(body) <= 5000), -- Markdown
  owner_id TEXT REFERENCES people(id), -- required to publish
  review_interval_days INTEGER DEFAULT 90 CHECK (review_interval_days IN (30, 90, 180, 365)), -- NULL means does not expire
  verified_at TIMESTAMP,
  verified_by TEXT REFERENCES people(id),
  verified_until DATE, -- date of verified_at in the team zone plus review_interval_days; NULL when it does not expire
  needs_verification TEXT CHECK (needs_verification IN ('not_verified', 'edited', 'restored')), -- NULL means empty
  needs_verification_by TEXT REFERENCES people(id),
  needs_verification_at TIMESTAMP,
  version INTEGER NOT NULL DEFAULT 0, -- 0 until first published, then the current version number
  created_by TEXT NOT NULL REFERENCES people(id),
  created_at TIMESTAMP NOT NULL,
  updated_by TEXT NOT NULL REFERENCES people(id), -- the author of the current version (the last save for drafts)
  updated_at TIMESTAMP NOT NULL,
  archived_by TEXT REFERENCES people(id),
  archived_at TIMESTAMP,
  reminder_sent_for DATE, -- the verified_until value the review_due notice was sent for
  overdue_sent_for DATE, -- the verified_until value the review_overdue notice was sent for
  CHECK (status = 'draft' OR owner_id IS NOT NULL),
  CHECK (status = 'draft' OR length(body) >= 1),
  CHECK ((status = 'draft' AND version = 0) OR (status <> 'draft' AND version >= 1)),
  CHECK (verified_at IS NOT NULL OR (verified_by IS NULL AND verified_until IS NULL))
);

-- An alternate phrasing of the question on a card (0 to 5 per card, in display order).
CREATE TABLE card_questions (
  card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE,
  position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 5), -- the 1 to 5 range caps a card at 5 questions
  question TEXT NOT NULL CHECK (length(question) BETWEEN 3 AND 120),
  PRIMARY KEY (card_id, position)
);

-- A tag on a card (0 to 8 per card, in display order).
-- App rule (R4): tags use only lowercase letters, digits and hyphens.
CREATE TABLE card_tags (
  card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE,
  position INTEGER NOT NULL CHECK (position BETWEEN 1 AND 8), -- the 1 to 8 range caps a card at 8 tags
  tag TEXT NOT NULL CHECK (length(tag) BETWEEN 2 AND 30 AND tag = lower(tag)),
  PRIMARY KEY (card_id, position),
  UNIQUE (card_id, tag)
);

-- A published version of a card, written on every publish and never changed or deleted (R6).
CREATE TABLE card_versions (
  id TEXT PRIMARY KEY,
  card_id TEXT NOT NULL REFERENCES cards(id),
  number INTEGER NOT NULL CHECK (number >= 1),
  title TEXT NOT NULL,
  body TEXT NOT NULL,
  questions TEXT NOT NULL DEFAULT '', -- the alternate questions as published, one per line, in order
  tags TEXT NOT NULL DEFAULT '', -- the tags as published, one per line, in order
  change_note TEXT NOT NULL DEFAULT '', -- empty for version 1, 3 to 200 characters after that
  author_id TEXT NOT NULL REFERENCES people(id),
  published_at TIMESTAMP NOT NULL,
  UNIQUE (card_id, number),
  CHECK ((number = 1 AND change_note = '') OR (number > 1 AND length(change_note) BETWEEN 3 AND 200))
);

-- A flag saying a card is wrong or out of date. An open flag makes the card Unverified.
-- App rule (R9): the owner of the card cannot flag it; only the person who flagged can withdraw it.
CREATE TABLE flags (
  id TEXT PRIMARY KEY,
  card_id TEXT NOT NULL REFERENCES cards(id),
  person_id TEXT NOT NULL REFERENCES people(id), -- who flagged
  reason TEXT NOT NULL CHECK (length(reason) BETWEEN 5 AND 500),
  created_at TIMESTAMP NOT NULL,
  status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'resolved')),
  resolved_at TIMESTAMP,
  resolved_by TEXT REFERENCES people(id),
  resolution TEXT CHECK (resolution IN ('verified', 'archived', 'withdrawn')),
  CHECK ((status = 'open' AND resolved_at IS NULL AND resolution IS NULL)
      OR (status = 'resolved' AND resolved_at IS NOT NULL AND resolution IS NOT NULL))
);
CREATE UNIQUE INDEX flags_one_open_per_person ON flags (card_id, person_id) WHERE status = 'open'; -- at most one open flag per person per card

-- A person following a card, told when a new version is published with "Notify followers".
CREATE TABLE follows (
  card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE,
  person_id TEXT NOT NULL REFERENCES people(id),
  created_at TIMESTAMP NOT NULL,
  PRIMARY KEY (card_id, person_id)
);

-- One action on a card. Append-only; rows go only when a draft is deleted with its card (R19).
CREATE TABLE card_events (
  id TEXT PRIMARY KEY,
  card_id TEXT NOT NULL REFERENCES cards(id) ON DELETE CASCADE,
  actor_id TEXT REFERENCES people(id), -- NULL for the system
  created_at TIMESTAMP NOT NULL,
  type TEXT NOT NULL CHECK (type IN ('created', 'published', 'updated', 'verified', 'flagged', 'flag_withdrawn', 'owner_changed', 'archived', 'restored')),
  data TEXT NOT NULL DEFAULT '{}' -- a JSON object stored as text: version number, old and new owner, flags resolved
);

-- One submitted search. Never stores who searched (R14). Rows older than 90 days are deleted by the daily job.
CREATE TABLE search_logs (
  id TEXT PRIMARY KEY,
  query TEXT NOT NULL CHECK (length(query) BETWEEN 1 AND 200), -- the search terms joined by single spaces
  result_count INTEGER NOT NULL CHECK (result_count BETWEEN 0 AND 20), -- results shown, at most 20
  created_at TIMESTAMP NOT NULL
);
CREATE INDEX search_logs_created_at ON search_logs (created_at);

-- One email in the outbox. Written in the same transaction as its action and sent after it commits (R17).
CREATE TABLE notifications (
  id TEXT PRIMARY KEY,
  kind TEXT NOT NULL CHECK (kind IN ('verify_request', 'card_updated', 'flagged', 'flag_resolved', 'review_due', 'review_overdue')),
  person_id TEXT NOT NULL REFERENCES people(id), -- the recipient
  to_email TEXT NOT NULL,
  card_id TEXT NOT NULL REFERENCES cards(id),
  subject TEXT NOT NULL,
  body TEXT NOT NULL, -- plain text
  status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'logged', 'failed')),
  attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), -- each send attempt adds 1; logging is not an attempt; retried until 5
  last_error TEXT,
  created_at TIMESTAMP NOT NULL,
  sent_at TIMESTAMP
);
CREATE INDEX notifications_status ON notifications (status);

Seed data

Loads all of section 15: the settings row, 5 teams, 11 people, 4 collections with their team and editor rows, 12 cards with their 7 alternate questions and 24 tags, 13 versions, 2 flags, 2 follows, 28 events and 7 search log rows. The outbox is empty, as the spec says. Times are converted from 10:00 New York time to UTC. The keys inside each event's data object (version, verified, flags_resolved) are our naming; the spec lists what data records but not its key names.

Download knowledge-base-seed.sql (126 rows). It loads with foreign keys enforced on both engines.

Load it

SQLite, into a new file:

sqlite3
curl -s https://personalsoftwarefactory.com/schemas/knowledge-base.sql https://personalsoftwarefactory.com/schemas/knowledge-base-seed.sql | sqlite3 knowledge_base.db

PostgreSQL, into a new database:

psql
createdb knowledge_base && curl -s https://personalsoftwarefactory.com/schemas/knowledge-base.sql https://personalsoftwarefactory.com/schemas/knowledge-base-seed.sql | psql -v ON_ERROR_STOP=1 knowledge_base

SQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.

Design decisions

Trust is computed, not stored

There is no verified or trust column. Section 6 and R12 say trust is worked out every time from the owner, open flags, needs_verification and the review date, in that order of rules. A stored flag would stay Verified the day after verified_until passes, or after the owner is deactivated, because nothing writes to the card on those days.

Verification fields are kept apart from the edit state

verified_at, verified_by and verified_until record the last verification, while needs_verification (edited, not_verified or restored, NULL for empty) records why the card is waiting since. Section 6 needs both: KB-104 was verified on 3 Aug and then edited by Sam, and the trust text names the editor and the owner. Clearing verified_at on an edit would lose the review date that the scheduled job's markers compare against.

Alternate questions and tags are child tables

Section 5 allows 0 to 5 alternate questions and 0 to 8 tags, and section 8 searches both as separate fields. card_questions and card_tags store one item per row with a per-item length check, a unique tag per card, and a position column checked to 1 to 5 or 1 to 8, which caps the count in the database. The tag character rule (lowercase letters, digits and hyphens) has no portable pattern check, so only lowercase and length are enforced in SQL.

Versions keep a text snapshot of questions and tags

From the build test · field report

A CardVersion is written once and never changed (R6), so its questions and tags are stored as text, one item per line, as they were when published. The seed follows the rule added after the build test found that the spec gave only the old body: earlier versions keep the card's current title, questions and tags and differ only in the body.

One open flag per person per card is a partial unique index

Section 5 allows at most one open flag per person per card, but a person may flag the same card again after a flag is resolved. A unique index on (card_id, person_id) WHERE status = 'open' enforces exactly that, while a plain unique constraint would block the second flag forever.

Verify now is one event, not two

From the build test · field report

card_events stores one row per action with a JSON object in a text column. The build test found that publishing with Verify now could be read as two events; R19 now says it is one published or updated event whose data records the verification and the number of flags resolved, and the seed's KB-101 version 2 event does that.

Deleting a draft cascades, nothing else does

Only drafts can be deleted, and section 7 says the card, its events and follows go with it, so those foreign keys use ON DELETE CASCADE. card_versions and flags have no cascade because only published cards have them. Collections and teams that are still used can't be deleted (R15), which the plain foreign keys from cards, people and collection_teams enforce.

Example queries

The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.

Verification score by collection

SQL
SELECT col.name AS collection,
  COUNT(*) AS published_cards,
  SUM(CASE WHEN o.active = TRUE
    AND (o.is_admin = TRUE OR EXISTS (SELECT 1 FROM collection_editors e WHERE e.collection_id = c.collection_id AND e.person_id = c.owner_id))
    AND NOT EXISTS (SELECT 1 FROM flags f WHERE f.card_id = c.id AND f.status = 'open')
    AND c.needs_verification IS NULL
    AND (c.review_interval_days IS NULL OR c.verified_until >= '2026-10-14') THEN 1 ELSE 0 END) AS verified_cards,
  (200 * SUM(CASE WHEN o.active = TRUE
    AND (o.is_admin = TRUE OR EXISTS (SELECT 1 FROM collection_editors e WHERE e.collection_id = c.collection_id AND e.person_id = c.owner_id))
    AND NOT EXISTS (SELECT 1 FROM flags f WHERE f.card_id = c.id AND f.status = 'open')
    AND c.needs_verification IS NULL
    AND (c.review_interval_days IS NULL OR c.verified_until >= '2026-10-14') THEN 1 ELSE 0 END) + COUNT(*)) / (2 * COUNT(*)) AS score_percent
FROM cards c
JOIN collections col ON col.id = c.collection_id
JOIN people o ON o.id = c.owner_id
WHERE c.status = 'published'
GROUP BY col.name
ORDER BY col.name;

On the seed data, with today as 2026-10-14: 4 rows; IT & Accounts has 5 published cards, 2 verified, 40%.

Needs review, grouped by owner

SQL
SELECT owner, card, title, reason FROM (
  SELECT o.name || CASE WHEN o.active = FALSE THEN ' (inactive)' ELSE '' END AS owner,
    c.number,
    'KB-' || c.number AS card,
    c.title,
    CASE
      WHEN o.active = FALSE OR (o.is_admin = FALSE AND NOT EXISTS (SELECT 1 FROM collection_editors e WHERE e.collection_id = c.collection_id AND e.person_id = c.owner_id)) THEN 'Needs a new owner'
      WHEN EXISTS (SELECT 1 FROM flags f WHERE f.card_id = c.id AND f.status = 'open') THEN 'Flagged'
      WHEN c.needs_verification IS NOT NULL THEN 'Waiting for verification: ' || c.needs_verification
      WHEN c.review_interval_days IS NOT NULL AND c.verified_until < '2026-10-14' THEN 'Review overdue'
    END AS reason
  FROM cards c
  JOIN people o ON o.id = c.owner_id
  WHERE c.status = 'published'
) needs_review
WHERE reason IS NOT NULL
ORDER BY owner, number;

On the seed data: 4 rows; Kai Brooks (inactive) owns KB-112, which needs a new owner.

Searches with no results in the last 30 days

SQL
SELECT query, COUNT(*) AS searches
FROM search_logs
WHERE result_count = 0
  AND created_at >= '2026-09-14 13:00:00'
GROUP BY query
ORDER BY COUNT(*) DESC, MAX(created_at) DESC;

On the seed data, with now as 2026-10-14 13:00 UTC: 3 rows; parking pass with 3 searches comes first.

Recently changed, for one reader

SQL
SELECT 'KB-' || c.number AS card, c.title, v.number AS version, a.name AS author,
  CASE WHEN v.number = 1 THEN 'Published' ELSE v.change_note END AS note
FROM card_versions v
JOIN cards c ON c.id = v.card_id
JOIN collections col ON col.id = c.collection_id
JOIN people a ON a.id = v.author_id
WHERE c.status = 'published'
  AND (col.visibility = 'everyone'
       OR EXISTS (SELECT 1 FROM collection_teams ct JOIN people me ON me.team_id = ct.team_id
                  WHERE ct.collection_id = col.id AND me.id = 'p-jo'))
ORDER BY v.published_at DESC
LIMIT 10;

On the seed data, for Jo Park: 10 rows; the first is KB-104 version 2 by Sam Okafor, Added the $500 yearly limit.

Cards due an overdue notice

SQL
SELECT 'KB-' || c.number AS card, o.name AS owner, o.email, c.verified_until
FROM cards c
JOIN people o ON o.id = c.owner_id
WHERE c.status = 'published'
  AND c.review_interval_days IS NOT NULL
  AND o.active = TRUE
  AND c.verified_until < '2026-10-14'
  AND (c.overdue_sent_for IS NULL OR c.overdue_sent_for <> c.verified_until)
ORDER BY c.number;

On the seed data: 1 row; KB-103, owned by Ben Cho, due 2026-10-04.

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 Internal Knowledge Base blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 31 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 31 of 31 acceptance checks in a browser.

Follow new blueprints

Every new blueprint and build test appears in the feed and in the public GitHub repository. No account, no email list.