# Database architecture

Core ownership chain:

users
 ├─ accounts
 ├─ categories
 ├─ transactions → account + category
 ├─ budgets → category
 ├─ contacts → loans → loan_payments
 ├─ goals
 ├─ recurring_transactions
 ├─ tags
 ├─ activity_logs
 └─ settings

All financial records carry `user_id` either directly or through a parent whose ownership is verified before mutation.

## Decimal policy
Use DECIMAL(18,2) for monetary values. Do not use float columns.

## Transaction consistency
A transaction and its account balance delta are committed in the same DB transaction. Balance mutation uses `lockForUpdate()` to reduce race conditions.

## Transfer policy
A future transfer module should create a paired debit/credit record linked by `transfer_group` and exclude both legs from income/expense/savings KPI totals.

## Loan policy
Loan principal and repayment state are separate from ordinary spending. A repayment should affect the account only once and update the loan's paid amount once.
