I'm creating accounting software. I need to enforce double entry bookkeeping. I have the classical problem of one row per transaction versus two rows.
Let's take an example and see how it would be implemented in both scenarios.
Consider account Cash
and account Rent
. When I pay my monthly rent, I transfer $100 from my Cash
account to me Rent
account.
One row per Transaction
In a one row system, such transaction would be stored as:
transactions
tx_id | posting_date
1 | 23/05/2015
transaction_records
id | tx_id | credit_account | debit_account | amount
1 | 1 | Cash | Rent | 100.00
Two rows per Transaction
In a two row system, I'd have to mirror the same transaction record to create an opposite record that once I sum up both, I'd get zero balance.
transactions
tx_id | posting_date
1 | 23/05/2015
transaction_records
id | tx_id | type | account | amount
1 | 1 | credit | Cash | 100.00
2 | 1 | debit | Rent | 100.00
The problem
First of all I'd like to note: the reason I have both transactions
and transaction_records
tables (instead of one table) is to be able to handle split transactions (a case where I transfer $100 from Cash
account to two or more different accounts).
At first I tried to implement this with one row per transaction, but its a pain to calculate the account balance, and to actually retrieve the data.
I'm leaning towards the second scenario; however, it also has some issues:
- How do I update a single record? Assuming I've made a mistake and instead of recording $100 for my rent, I've recorded $10. I now have 2
transaction_records
- one for credit and one for debit, both with amount $10. - Now I do my reconciliation and I want to fix this typo. How would I fix this in the database? I don't know the connection between records, and in case of a split, one transaction can have more than 2 records. The only solution I came up with is to add some
ref_id
for each records pair that will uniquely identify those records as being the "opposite sides of each other" inside a context of a specifictx_id
.
Which approach is better/simpler?
To simplify my question: I want to represent a movement of funds from account A to account B. The two scenarios I gave are both valid designs to store such transaction. As I also pointed out they both have cons & pros (first one: easier to save, harder to retrieve; second one the opposite).
They might have other pros/cons that I do not spot right now, hence I ask an opinion from more experienced people.