I have a postgres db table connected to an application that runs validation checks before "running". These validations often fail due to incorrect default constraints (over or under the min max values). Instead of manually updating these, I'd like to run a query to find any default values that are either over or under the min/max and have them update to an arbitrary number in between (the application is self correcting so it doesn't matter where it lands).
Here is what I have but it isn't running:
do $$
begin
for i in (select id,* from constraints_table)
loop
if i.default_value < i.min_value or i.default_value>i.max_value
then
update constraints_table
set i.default_value = ((i.min_value + i.max_value)/2)
where id=i.id;
end if;
end loop;
end; $$
a) can someone help fix this query
b) is there an easier way? (python script?)