I'm new to triggers in PostgreSQL.
I have 3 tables in this example table1, table2 and table3.
- The trigger fires on new record on
table1. - The trigger function loops through all records of
table2that have the sameproduct_idas the new record fromtable1. - It inserts into
table3some mixed values from the first 2 tables. - After that, the same
INSERTquery is inserted into a test table for testing purpose.
The problem is the INSERT on table3 is not happening. The INSERT on test table is fine and the logged insert query can be executed without troubles, so I don't know why it's not executed inside the trigger/function.
CREATE OR REPLACE FUNCTION my_trigger() RETURNS TRIGGER AS $my_trigger$
DECLARE r RECORD;
BEGIN
FOR r IN SELECT t2.id_t2, t2.name_1, t2.name_2, t2.name_3 FROM table2 t2 WHERE t2.product_id=NEW.product_id
LOOP
EXECUTE 'INSERT INTO table3 (id_t3, id_t1, name_1, name_2, name_3, bool_t2) VALUES (' || r.id_t2 || ',' || NEW.id_t1 || ', ''' || r.name_1 || ''',''' || r.name_2 || ''',''' || r.name_3 || ''', TRUE);';
INSERT INTO test (field1, field2) VALUES(r.id_t2, 'INSERT INTO table3 (id_t3, id_t1, name_1, name_2, name_3, bool_t2) VALUES (' || r.id_t2 || ',' || NEW.id_t1 || ', ''' || r.name_1 || ''',''' || r.name_2 || ''',''' || r.name_3 || ''', TRUE);');
END LOOP;
RETURN NEW;
END;
$my_trigger$ LANGUAGE plpgsql;
Edit: As @Rachcha asked, the trigger itself is defined this way:
CREATE TRIGGER my_trigger
AFTER INSERT
ON table1
FOR EACH ROW
EXECUTE PROCEDURE my_trigger();
Edit 2: I've also tried to insert without EXECUTE, but the result is the same: no errors but no fields inserted into table3.