title: Trigger to delete rows from related tables before deleting rows from actual table
body: <p>I have the following tables:</p>
  
  <pre><code>CREATE TABLE QUESTION(
  id varchar(10) NOT NULL PRIMARY KEY,
  que_type numeric(1));
  
  CREATE TABLE ESSAY(
  que_id varchar(10) NOT NULL PRIMARY KEY,
  ans varchar(2000),
  FOREIGN KEY (que_id) REFERENCES QUESTION (id));
  
  CREATE TABLE TFFB(
  que_id varchar(10) NOT NULL PRIMARY KEY,
  ans varchar(50),
  FOREIGN KEY (que_id) REFERENCES QUESTION (id));
  
  CREATE TABLE MCQ(
  que_id varchar(10) NOT NULL PRIMARY KEY,
  ans varchar(200),
  FOREIGN KEY (que_id) REFERENCES QUESTION (id));
  </code></pre>
  
  <p>and try to create trigger so that when I delete from the main table, it will delete related rows from other tables:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION delete_question()
  RETURNS trigger AS $delete_question$
  DECLARE
      BEGIN
      IF ( (OLD).que_type = '1' ) THEN
         IF EXISTS (SELECT 1 FROM mcq WHERE person_id = (OLD).id) THEN
            DELETE FROM mcq WHERE que_id='(OLD).id';
         END IF;
      ELSIF ( (OLD).que_type = '2' OR OLD.que_type = '3' ) THEN
         IF EXISTS (SELECT 1 FROM tffb WHERE person_id = (OLD).id) THEN
            DELETE FROM tffb WHERE que_id='(OLD).id';
         END IF;
      ELSIF ( (OLD).que_type = '4' ) THEN
         IF EXISTS (SELECT 1 FROM essay WHERE person_id = (OLD).id) THEN
            DELETE FROM essay WHERE que_id='(OLD).id';
         END IF;
      END IF;
      RETURN NULL;
      END;
      $delete_question$ LANGUAGE plpgsql;
  
      CREATE TRIGGER delete_question
      BEFORE DELETE ON question
          FOR EACH ROW EXECUTE PROCEDURE delete_question();
  </code></pre>
  
  <p>When I delete data from <code>question</code>, the row disappear for a while. But when I refresh, it still there.<br>
  I tried to put <code>RETURN OLD;</code> but it failed because of the constrain relation.
  What is wrong with this?</p>