HappyCry
HappyCry

Reputation: 893

Inserting into Postgres within a trigger function

I have a trigger function that I am running BEFORE INSERT on table1. In this function I do the following:

INSERT INTO table2 VALUES(x, y, z);
INSERT INTO table3 VALUES(x, a);

For some reason, these INSERT statements do not run. I know that the function is being called.

UPDATE:

More information, my trigger looks like this:

CREATE TRIGGER update_db
BEFORE INSERT ON table1
FOR EACH ROW
EXECUTE PROCEDURE update_all_db();

My procedure function doesn't do much. All I'm trying to do are a few statements as above.

Thank you in advance.

Upvotes: 0

Views: 146

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656291

Semicolons (;) are missing behind your statements.

And it is a suicidal move not to include a column list for your table in a persisted INSERT statement. If you change the table definition of table2 or table3 in the future, the trigger silently breaks or destroys data in the worst case. Should be:

INSERT INTO table2(col1, col2, col3) VALUES(x, y, z);
INSERT INTO table3(col1, col2) VALUES(x, a);

But that's no reason the statement should just "not run".
This problem is due to something that is not in your question.

Upvotes: 1

Related Questions