sql - prevent any update or delete from table -


i have table added boolean column done when column true want prevent unautorized action (delete, update) on row.

i wrote trigger:

create trigger productstock_beforeupdate   before update or delete   on table   each row   execute procedure trig()  create or replace function trig()   returns trigger $$ begin     if tg_op='update' or tg_op='delete'         if old.done=true                     raise exception 'cant that';             return null;         end if;     end if;     return new; end; $$ 

if autorized transaction wants change data have first disable trigger.

this idea should work when try delete row

done=false 

it returns me "0 rows deleted" , not perform delete.

any idea wrong trigger?

this caused fact update go through need return new record. delete go through, need return oldrecord.

quote manual

thus, if trigger function wants triggering action succeed without altering row value, new (or value equal thereto) has returned [...] note new null in delete triggers, returning not sensible. usual idiom in delete triggers return old.

so trigger function should this:

create or replace function trig()   returns trigger $$ begin   -- no need check action here trigger   -- fire update or delete   if old.done     raise exception 'cant that';     return null;   end if;    if tg_op='update'      return new;   end if;     if tg_op='delete'     return old;   end if; end; $$ language plpgsql; 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -