我必須在postgres中編寫觸發器 觸發器應該在rp_resourceUser表中插入值。postgres觸發器在條件時插入值
我有三個表rp_user,rp_resourceUser,rp_resourceType他們都在public模式
fields in rp_user
user_id bigint NOT NULL,
is_gtl boolean,
is_tsc_admin boolean,
username character varying(255) NOT NULL,
gtl_user_id bigint,
fields in rp_resource_type
resource_type_id bigint NOT NULL,
resource_template text,
resource_type character varying(255),
fields in rp_resource_user_type
resource_user_type_id bigint NOT NULL,
gtl_user_id bigint NOT NULL,
resource_type_id bigint NOT NULL,
user_id bigint NOT NULL,
CONSTRAINT rp_resource_user_type_pkey PRIMARY KEY (resource_user_type_id),
CONSTRAINT fk6t2cd10tfar76hvvchyo81u66 FOREIGN KEY (user_id)
REFERENCES public.rp_user (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fkb273dt5c7b9s49jlwf1cmbxib FOREIGN KEY (gtl_user_id)
REFERENCES public.rp_user (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fkmbxelxexffw96mcgcu3y25ixl FOREIGN KEY (resource_type_id)
REFERENCES public.rp_resource_type (resource_type_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uk994khkskqm5c77mlyipwxu0dv UNIQUE (user_id, gtl_user_id, resource_type_id)
所以當用戶表GTL標誌rp_resource_user_type
我已經被設置爲比插入值條件觸發會寫這樣的事情
CREATE TRIGGER gtlPermission AFTER UPDATE ON rp_user.is_gtl
FOR EACH ROW
WHEN (public.rp_user.is_gtl IS true)
EXECUTE PROCEDURE setGtlPermission();
Create or Replace Function setGtlPsermission() RETURNS TRIGGER AS $rp_resource_user_type$
BEGIN
INSERT INTO rp_resource_user_type(resource_user_type_id, gtl_user_id, resource_type_id,user_id)
VALUES (new.resource_user_type_id, rp_user.gtl_user_id, rp_resource_type.resource_type_id ,rp_user.user_id);
RETURN NEW;
END;
$rp_resource_user_type$ LANGUAGE plpgsql;
我從來沒有寫過觸發器之前,所以我不知道如果這就是正確的..所以需要建議
這裏有一個教程:http://www.postgresqltutorial.com/introduction-postgresql-trigger/這有幫助嗎? –