0

I am trying to write a rule that on insert if there is conflict to do nothing. The hard part is that I am using java code for interacting with the db and the save method expects an instance to be returned. So the goal I am trying to achieve is, a rule that triggers on conflict when inserting, does nothing and returns the given instance. I have tried to do something like that :

CREATE RULE "my_rule" AS ON INSERT TO "table_x"
WHERE EXISTS(SELECT 1 FROM table_x WHERE id = NEW.id)
DO INSTEAD
Select * from table_x WHERE(id=NEW.id);
NailP
  • 1

2 Answers2

0

You don't need a rule for that, use instead :

INSERT ... ON CONFLICT DO NOTHING

see the manual

Edouard
  • 6,577
  • 1
  • 9
  • 20
0

I never use Postgresqls rules, but I'd expect that you could combine it with the idea of Edouards answer.

CREATE RULE "my_rule" AS ON INSERT TO "table_x"
DO INSTEAD
INSERT ... ON CONFLICT DO NOTHING;
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348