I want to replace INSERT into a table with a PostgreSQL rule.
Here is my two tables:
Resource:
uid (PK): UUID
type (NOT_NULL): ENUM
SpecificResource:
uid (PK, FK on resource.uid): UUID
content (NOT_NULL): JSONB
I want any user to the database to be able to make insert/update/delete on SpecificResource directly without the need to insert/update/delete on Resource.
Here is my unsuccessful try that triggers an infinite recursion loop (indeed because I try to re-insert in specific_resource table with a RULE (...) DO INSTEAD :
CREATE OR REPLACE RULE insert_specific_resource
AS ON INSERT TO specific_resource
DO INSTEAD (
INSERT INTO resource (uid, type)
VALUES (NEW.uid, 'SPECIFIC_RESOURCE');
INSERT INTO specific_resource (uid)
VALUES (new.uid)
);
instead oftrigger?resourcetable. However, it does not insert anything inspecific_resourcetable onINSERT INTO specific_resource ...CREATE OR REPLACE FUNCTION add_specific_resource() RETURNS TRIGGER AS $BODY$ BEGIN INSERT INTO resource(uid, type) VALUES (uid, 'SPECIFIC_RESOURCE'); RETURN NEW; END $BODY$ LANGUAGE PLPGSQL; CREATE OR REPLACE TRIGGER add_dashboard BEFORE INSERT ON dashboard FOR EACH ROW EXECUTE PROCEDURE add_specific_resource();