0

I have two tables

  1. Main table

  2. Sub table

My intention is to use a trigger function to insert the new row into the Sub Table if it fulfils a condition of a column not being Null.

I have tried this method but my Sub Table will still insert the row with a null value

BEGIN
    INSERT INTO unsubscriber_list("ID","Name","Email","unsubdate")
    SELECT new."ID",new."Name",new."Email",new."date_of_unsub" FROM master_database
    WHERE date_of_unsub IS NOT NULL
    ORDER BY "account_created_on" DESC LIMIT 1;
    RETURN NULL;
END;

I have also tried an IFTHEN condition but it produces error

BEGIN
    IF master_database.date_of_unsub IS NOT NULL THEN
    INSERT INTO unsubscriber_list("ID","Name","Email","unsubdate")
    SELECT new."ID",new."Name",new."Email",new."date_of_unsub" FROM master_database;
    END IF;
    RETURN NULL;
END;

This is the error that they showed

ERROR:  missing FROM-clause entry for table "master_database"
LINE 1: SELECT master_database.date_of_unsub IS NOT NULL

My expected result is one whereby an Insert Trigger activates the Trigger Function whereby it validates if that specific column IS NOT NULL

If Null: ignore If not Null: Insert.

5
  • You have an alias label named "new" but didn't define it. Commented May 7, 2019 at 2:17
  • Hi Philip! can you explain more please? I'm a total newbie at this Commented May 7, 2019 at 2:19
  • Both of your queries would trigger errors because of the undefined table alias name "new". However according to your description the first one (partly) worked. I'm wondering if you modified it a little bit and missed some part. Commented May 7, 2019 at 2:22
  • Philip, thank you for taking time out to solve my problem. I've googled a little bit more about New and it seems that it is a special variable that is auto-created. New Data type RECORD; variable holding the new database row for INSERT/UPDATE operations in row-level triggers. This variable is null in statement-level triggers and for DELETE operations. I played around with my code with this new knowledge and solved the problem. I will be posting the answer when i tidy up the code. Thanks again! Commented May 7, 2019 at 2:29
  • 1
    @PhilipTzou: the new record is automatically available in a trigger function. Commented May 7, 2019 at 5:47

1 Answer 1

1

After playing around with my code, it seems that I have solved the problem.

I used the IFTHEN function and added a NEW (which according to postgresql, is a special variable that is automatically created for a trigger function to hold the newly created row.)

this is my code

BEGIN
    IF NEW.date_of_unsub IS NOT NULL THEN
    INSERT INTO unsubscriber_list("ID","Name","Email","unsubdate")
    SELECT NEW."ID",NEW."Name",NEW."Email",NEW."date_of_unsub" FROM master_database;
    END IF;
    RETURN NULL;
END;

I have tested the function by inserting a script and it seems to create the appropriate row while ignoring inappropriate ones.

Then again, I'm a total newbie at Postgresql so I might not have the nicest code. Will be looking out for cleaner ways to produce such a function!

Sign up to request clarification or add additional context in comments.

2 Comments

The function looks alright, except you should RETURN NEW.
Why are you selecting NEW values from master_database? The master_database table (I assume that's what it is) has nothing to do with the NEW row variable. If you want to select NEW values, just do INSERT INTO ... VALUES (NEW."ID", NEW."Name", ...). Also indent the code within the IF block for readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.