Postgres trigger function

后端 未结 1 1621
陌清茗
陌清茗 2020-12-31 12:11

I need help in Postgres triggers.

I have table with 2 columns:

sold boolean;
id_shop int;

I

1条回答
  •  情话喂你
    2020-12-31 12:46

    First of all you need a before trigger if you want to change a value of the row being updated (or inserted)

    Secondly you don't need to "update" the table, just assign the new value to the NEW row:

    create or replace function pardota_masina_veikals() 
    RETURNS trigger 
    AS 
    $pardota_masina$
    begin
      IF NEW.sold=true THEN
        NEW.id_shop = NULL;
     END IF;
    RETURN NEW;
    END;
    $pardota_masina$ 
    LANGUAGE plpgsql;
    
    CREATE TRIGGER pardota_masina_nevar_but_veikala 
       BEFORE INSERT OR UPDATE ON masinas 
       FOR EACH ROW EXECUTE PROCEDURE pardota_masina_veikals();
    

    0 讨论(0)
提交回复
热议问题