How to automatically update model field if there are changes to the previous field?

am trying to find a way to update the latest field in a model if there are changes in the earlier field.

for example: say I have a Cash Balance model

Cash Balance

If I change the sale inv 134 debits to 50. how do I make this change reflect on the other cash balance coming after it automatically?

“Automatically”? You don’t.

You would need to write the code to perform an update on the rows “after” the row being updated. (You could do this in the save method of your CashBalance model.)

(Hopefully you have a way to specifically identify the sequence of rows so as to know in what order they are being maintained.)

Would it be possible to equate CashBalance to an equation that uses the CB(Cashbalance) before that like how we can do in excel? save method?

No, a database is not a spreadsheet.

Actually, my original reaction to the issue you presented is that your model may not be the best design for what you’ve described.

Either I wouldn’t design the models that way, or I wouldn’t allow for a previous invoice to be altered. (The historical, traditional bookkeeping method of doing something like that would be to create two new entries. You create an entry to reverse the original invoice and then create a new entry with the corrected value. You never change a previously logged entry. Auditors never like that.)

So deleting a miscellaneous entry would be out of the question if it isn’t the last entry right?

That strictly depends upon the requirements for your system. It really comes down to what business function(s) this system is designed to support.

If you’re a company that requires any kind of transactional audit (potentially including a tax audit), then deleting any entry is a no-no.

If you’re an individual keeping track of your personal finances, then pretty much anything goes.

If it was a personal financial model what would be the best way to approach this, like for example say

Discripton debit credit cashbalance
Opening 901
salary 150 1051
books 42 1009
Friend 1 450 559
closing 559

say the friend 1 comes back for another fifty.

What would be the best way to update friend 1 credit and the CB? Do I delete and redo friend 1 and closing or is there any other way?

I’d add another transaction.

Don’t mix the concepts of what you’re storing in the database with what gets displayed on the page. Most importantly, don’t fall into the mindset that what you want to see on a page is necessarily representative of how the data is stored.

thank you so much for the advice and the response. It really was helpful.