I have a table called car
and another table called inventory
.
car
has a column called needs_oil_change
(bool)
inventory
has a column called oil_change_due_count
INTEGER
I want to create a trigger that will incremented or decrement the inventory.oil_change_due_count
whenever a car.oil_change_due_count
is changed (or whenever a new car record is inserted, or deleted).
So, in short, I want a trigger to keep the inventory summary count column oil_change_due_count
synchronized as car records are created/deleted/updated.
I've tried to follow some docs online like these:
https://w3resource.com/PostgreSQL/postgresql-triggers.php
https://dataegret.com/2017/10/postgresqls-transition-relations/
and others.
I haven't been able to create one that works yet.
How would I write a trigger that could handle that logic?
I just found this:
PostgreSQL: Checking for NEW and OLD in a function for a trigger
not sure if it will answer my question, but I will try to learn from that and see if I can apply to my question.