I am trying to create an function which will increment my version column value (int) if I update any column of my database row. For instance, if i have a foo table that has columns (id, title, version) and the default version is 1, I want my vesrion to be incremented once I update a title of my single foo row. I wrote the following function but it throws an error once I test it:
create or replace function update_foo_version()
returns trigger as $body$
begin
update foo
set version = version::int + 1
where old.id=new.id;
return new;
end
$body$
language plpgsql;
My trigger for this function:
create trigger update_version
after update on foo
for each row execute procedure update_foo_version();