I got a trouble when a try to execute a trigger. Let's suppose we have 2 tables and I want to copy data from table A to table B but each table got a unique constraint.
create table test1 (
test_name varchar);
create unique index test1_uc on test1 USING btree (test_name);
create table test2 (
test_name2 varchar);
create unique index test2_uc on test2 USING btree (test_name2);
CREATE OR REPLACE FUNCTION trig_test()
RETURNS trigger AS
$$
BEGIN
IF pg_trigger_depth() <> 1 THEN
RETURN NEW;
END IF;
INSERT INTO test2(test_name2)
VALUES(NEW.test_name2)
ON CONFLICT (test_name2) DO NOTHING;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trigger_test
AFTER INSERT
ON test2
FOR EACH ROW
EXECUTE PROCEDURE trig_test();
insert into test2 values ('test');
insert into test2 values ('test'); //should do nothing ?
But I get this error:
ERROR: duplicate key value violates unique constraint "test2_uc" DETAIL: Key (test_name2)=(test) already exists.
What's wrong with the trigger?