Designing the BudgetBuddy Schema
Requirements
Users can: track expenses and income across several accounts (cash, bank, eSewa), categorize them, set monthly budgets per category, track bills and recurring payments, loans/EMIs and savings goals, and see reports.
Entities
| Table | Purpose |
|---|---|
profiles | app user (linked to the auth user via UUID) |
accounts | cash, bank, wallet; current balance |
categories | default + user-defined, with parent/child |
transactions | income and expenses (type column), linked to an account |
budgets | limit per user + category + month |
bills | recurring bills with due day and frequency |
loans / loan_payments | EMI tracking |
savings_goals | target, saved amount, deadline |
Key decisions
- One
transactionstable withtype IN ('income', 'expense', 'transfer')and a positive amount → simpler reports than separate tables - Money:
NUMERIC(12,2), always positive, sign decided by type - Ownership:
user_id UUID NOT NULLon every user-owned table, indexed, protected by RLS - Categories:
user_id NULL= a built-in default visible to everyone; otherwise private - Balances: derived from transactions (a view), or a cached
balancekept in sync by a trigger - Dates:
occurred_on DATEfor the user-facing date,created_at TIMESTAMPTZfor the system time - Constraints everywhere: CHECKs on amounts, types and dates; UNIQUE (user, category, month) for budgets
- Indexes on
(user_id, occurred_on)for the most common query: "my transactions this month"
Typical queries the design must support
- This month's spending by category vs budget
- Account balances
- Upcoming bills in the next 7 days
- Monthly trends (income vs expense)
- Savings progress
The code example is a complete, runnable schema plus a budget-vs-actual report.
Example
CREATE TABLE bb_profiles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), -- = auth.users.id in Supabase
full_name TEXT NOT NULL,
currency CHAR(3) NOT NULL DEFAULT 'NPR',
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE bb_accounts (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id UUID NOT NULL REFERENCES bb_profiles(id) ON DELETE CASCADE,
name TEXT NOT NULL,
kind TEXT NOT NULL CHECK (kind IN ('cash', 'bank', 'wallet', 'card')),
opening_balance NUMERIC(12,2) NOT NULL DEFAULT 0,
UNIQUE (user_id, name)
);
CREATE TABLE bb_categories (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id UUID REFERENCES bb_profiles(id) ON DELETE CASCADE, -- NULL = built-in default
parent_id BIGINT REFERENCES bb_categories(id),
name TEXT NOT NULL,
kind TEXT NOT NULL CHECK (kind IN ('income', 'expense')),
icon TEXT
);
CREATE TABLE bb_transactions (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id UUID NOT NULL REFERENCES bb_profiles(id) ON DELETE CASCADE,
account_id BIGINT NOT NULL REFERENCES bb_accounts(id) ON DELETE CASCADE,
category_id BIGINT REFERENCES bb_categories(id) ON DELETE SET NULL,
type TEXT NOT NULL CHECK (type IN ('income', 'expense')),
amount NUMERIC(12,2) NOT NULL CHECK (amount > 0),
note TEXT,
occurred_on DATE NOT NULL DEFAULT CURRENT_DATE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX ON bb_transactions (user_id, occurred_on DESC);
CREATE TABLE bb_budgets (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id UUID NOT NULL REFERENCES bb_profiles(id) ON DELETE CASCADE,
category_id BIGINT NOT NULL REFERENCES bb_categories(id) ON DELETE CASCADE,
month DATE NOT NULL CHECK (month = date_trunc('month', month)), -- always the 1st
limit_amount NUMERIC(12,2) NOT NULL CHECK (limit_amount > 0),
UNIQUE (user_id, category_id, month)
);
CREATE TABLE bb_savings_goals (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id UUID NOT NULL REFERENCES bb_profiles(id) ON DELETE CASCADE,
title TEXT NOT NULL,
target NUMERIC(12,2) NOT NULL CHECK (target > 0),
saved NUMERIC(12,2) NOT NULL DEFAULT 0 CHECK (saved >= 0),
deadline DATE
);
-- Sample data
INSERT INTO bb_profiles (id, full_name) VALUES ('11111111-1111-1111-1111-111111111111', 'Asha Sharma');
INSERT INTO bb_accounts (user_id, name, kind, opening_balance)
VALUES ('11111111-1111-1111-1111-111111111111', 'Bank', 'bank', 50000);
INSERT INTO bb_categories (name, kind, icon) VALUES ('Food', 'expense', '🍛'), ('Salary', 'income', '💼'), ('Transport', 'expense', '🚌');
INSERT INTO bb_transactions (user_id, account_id, category_id, type, amount, occurred_on) VALUES
('11111111-1111-1111-1111-111111111111', 1, 2, 'income', 60000, '2026-11-01'),
('11111111-1111-1111-1111-111111111111', 1, 1, 'expense', 7200, '2026-11-05'),
('11111111-1111-1111-1111-111111111111', 1, 3, 'expense', 1800, '2026-11-12');
INSERT INTO bb_budgets (user_id, category_id, month, limit_amount) VALUES
('11111111-1111-1111-1111-111111111111', 1, '2026-11-01', 8000),
('11111111-1111-1111-1111-111111111111', 3, '2026-11-01', 1500);
-- Budget vs actual for November
SELECT c.icon || ' ' || c.name AS category,
b.limit_amount,
COALESCE(sum(t.amount), 0) AS spent,
round(100.0 * COALESCE(sum(t.amount), 0) / b.limit_amount) AS pct,
CASE WHEN COALESCE(sum(t.amount), 0) > b.limit_amount THEN '🔴 over' ELSE '🟢 ok' END AS status
FROM bb_budgets b
JOIN bb_categories c ON c.id = b.category_id
LEFT JOIN bb_transactions t
ON t.category_id = b.category_id AND t.user_id = b.user_id AND t.type = 'expense'
AND t.occurred_on >= b.month AND t.occurred_on < b.month + INTERVAL '1 month'
WHERE b.month = '2026-11-01'
GROUP BY c.icon, c.name, b.limit_amount
ORDER BY pct DESC;
-- 🚌 Transport | 1500 | 1800 | 120 | 🔴 over
-- 🍛 Food | 8000 | 7200 | 90 | 🟢 ok
-- Account balance = opening + income − expenses
SELECT a.name,
a.opening_balance
+ COALESCE(sum(t.amount) FILTER (WHERE t.type = 'income'), 0)
- COALESCE(sum(t.amount) FILTER (WHERE t.type = 'expense'), 0) AS balance
FROM bb_accounts a LEFT JOIN bb_transactions t ON t.account_id = a.id
GROUP BY a.id; -- Bank | 101000.00