0

Hello I am having the same problem with a trigger, I do not understand my error. The script compiles but when I delete an element I have this error



CREATE OR REPLACE FUNCTION delete_foreign_key() RETURNS TRIGGER AS $_$

  BEGIN 
       
          delete from planning.ordrelivraison_history 
          where ordrelivraison_history.ordrelivraisonid = old.planning.ordrelivraison.ordreid;
      RETURN OLD;
   END;


$_$ LANGUAGE plpgsql;


Create trigger delete_foreign_key_in_order_delivery_history
  after delete 
     on planning.ordrelivraison_history 
    for each row
execute procedure delete_foreign_key();

------------------------------------------------------

delete
from planning.ordrelivraison

where ordreid = 1656


Voici l'erreur

ERROR: ERROR: UPDATE or DELETE on the "delivery order" table violates the foreign key constraint "Ordrelivraison_history_ordreid" from the "ordrelivraison_history" table DETAIL: The key (orderid) = (1656) is always referenced from the “ordrelivraison_history” table.

SQL state: 23503

1
  • What is the error you get? One obvious error is that you forgot the "closing" $_$ for the function body. Commented Oct 8, 2021 at 14:38

1 Answer 1

1

Your trouble in you are firing the trigger at the wrong time. You declare it as AFTER delete but that makes it an after statement trigger, unfortunately the trigger function does not get the old/new rows on after statement trigger. See CREATE TRIGGER documentation. Change your trigger to:

Create trigger delete_foreign_key_in_order_delivery_history
   after delete 
      on ordrelivraison_history 
     for each row
 execute procedure delete_foreign_key();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.