Accounting for DevelopersModule 2: Data model and designIntermediate3 min read
Chart of Accounts Database Design for Developers
How to model a chart of accounts in a database: account types, normal balances, hierarchy, codes, currencies, closing accounts and multi-entity design, with SQL.
Course outline (16 lessons)
Before this lesson:Chart of AccountsBuilding a Double-Entry Accounting Engine in Code
In this lesson · 7 sections
The chart of accounts is the reference data every posting points to. Getting its model right early saves painful migrations later.
Core attributes
Each account needs:
- a stable id (never reused);
- a human-facing code (such as
1100) and name; - a type: asset, liability, equity, revenue or expense;
- its normal balance (debit or credit), usually derived from type but explicit for contra accounts;
- a currency, if your ledger is multi-currency;
- an active flag;
- an optional parent for hierarchical reporting.
CREATE TYPE account_type AS ENUM ('asset', 'liability', 'equity', 'revenue', 'expense');
CREATE TYPE normal_side AS ENUM ('debit', 'credit');
CREATE TABLE accounts (
id uuid PRIMARY KEY,
entity_id uuid NOT NULL REFERENCES entities(id),
code text NOT NULL,
name text NOT NULL,
type account_type NOT NULL,
normal_balance normal_side NOT NULL,
currency char(3) NOT NULL,
parent_id uuid REFERENCES accounts(id),
is_postable boolean NOT NULL DEFAULT true, -- false for header/group accounts
is_active boolean NOT NULL DEFAULT true,
created_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (entity_id, code)
);
Type vs normal balance
Normal balance usually follows type: assets and expenses are debit-normal; liabilities, equity and revenue are credit-normal. Contra accounts break the pattern: Accumulated Depreciation is an asset-type account with a credit normal balance. Storing both fields keeps reports correct.
Hierarchy and grouping
Reports group accounts: “Current assets” contains Cash, Receivables and Inventory. Two common approaches:
- Parent pointers (
parent_id) with header accounts that cannot receive postings (is_postable = false). Roll-ups are recursive queries. - Separate reporting mappings: a table that maps accounts to report lines. This lets you produce different layouts (management vs statutory) without restructuring the chart.
Many systems use both.
Rules worth enforcing
- Postings only to postable, active accounts. Check in the posting path and in a database constraint or trigger.
- No deleting accounts with postings. Deactivate instead. Historical reports must still resolve the account.
- Type changes are dangerous. Changing an account from expense to asset after it has postings silently rewrites history. Block it, or require a migration with reclassification entries.
- Codes are labels, ids are identity. Renumbering a code should not break references.
Subledger accounts and dimensions
Do not create a general ledger account per customer. Use a control account, such as Accounts Receivable, plus a counterparty or dimension on the posting:
ALTER TABLE postings ADD COLUMN counterparty_id uuid; -- customer, supplier, seller
ALTER TABLE postings ADD COLUMN dimensions jsonb; -- department, project, region
Dimensions let finance slice reports without exploding the chart of accounts. Validate allowed dimension values; free-form tags become unreportable quickly.
Multi-entity and multi-currency
- If you operate several legal entities, scope accounts by
entity_id. Each entity’s books must balance on their own. - In a multi-currency ledger, either give each account a single currency (simple and strict) or allow multi-currency accounts with per-currency balances. Converting between currencies is a separate, explicit entry using a recorded exchange rate, never an implicit conversion.
Recap
- Store type, normal balance, currency, active and postable flags explicitly.
- Use parent pointers and/or reporting mappings for hierarchy.
- Never delete or retype accounts with history.
- Use dimensions and counterparties instead of exploding the chart.
Active recall
Quick check · Test yourself
Work out each answer in your head first, then reveal the explanation and mark whether you got it right.
1Why store the account type rather than inferring it from the account code range?
Show worked answer
Code ranges are a convention that can vary or change. An explicit type makes reporting logic reliable and independent of numbering.
2A user wants to delete an account with historical postings. What should the system do?
Show worked answer
Refuse the delete and offer to deactivate the account instead, so history and past reports remain intact.
Key terms in this lesson
Lesson 3 of 16 · Accounting for Developers
Finished this lesson?
Mark it complete to track your progress through the course. Progress is saved in this browser only.
Previous lesson: Building a Double-Entry Accounting Engine in Code
Connected lessons
Educational content only—not accounting, tax, legal or investment advice. See our editorial policy or report an error via our corrections policy.