简体   繁体   English

在 postgres 中触发以使用列表锁定表

[英]Trigger in postgres to lock table with a list

I have an issue regarding a Triggers with PostgreSQL 10. Here is the situation.我有一个关于使用 PostgreSQL 10 的触发器的问题。情况如下。
I have a table called index_name which contain à column named index_ref.我有一个名为 index_name 的表,其中包含一个名为 index_ref 的列。 Into this field I have created a list of value as example: PBM PI, PBM PO, etc.在此字段中,我创建了一个值列表,例如:PBM PI、PBM PO 等。
I would like to use this table to store valid name as reference for another table called gis_osm_places.我想使用此表来存储有效名称作为另一个名为 gis_osm_places 的表的参考。 So, whenever someone try to insert a value not into the list then an exception message will pop-up to say:所以,每当有人试图插入一个不在列表中的值时,就会弹出一条异常消息说:
NOT ALLOWED COMMIT.不允许提交。 PLEASE USE: (reference list)请使用:(参考清单)

Here are my tables:这是我的表:
在此处输入图像描述

Here is where I am with the trigger:这是我使用触发器的位置:

CREATE FUNCTION public.check_column_value()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF

AS $BODY$

DECLARE
ref_allowed character varying;
BEGIN

IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE'
THEN

IF NEW.index_a is not null OR NEW.index_a NOT IN (SELECT index_ref from public.index_name)
THEN

ref_allowed := (SELECT string_agg(index_ref,',') from public.index_name);
RAISE EXCEPTION 'NOT ALLOWED COMMIT. PLEASE USE : %',ref_allowed;

END IF;
RETURN NEW;

END IF;
END;

$BODY$;

ALTER FUNCTION public.check_column_value()
    OWNER TO "postgres";

CREATE TRIGGER check_column_value
    BEFORE INSERT
    ON public.gis_osm_places
    FOR EACH ROW
    EXECUTE PROCEDURE public.check_column_value();

Actually nothing is happening, I mean I can add whatever I want without an error.实际上什么都没有发生,我的意思是我可以添加任何我想要的东西而不会出错。
Any idea or upgrade of the code would be greatly appreciate.任何想法或代码升级将不胜感激。
Thanks in advance !提前致谢 !

You do not need not and do not want a trigger for this.您不需要也不想为此触发。 This functionality is build in. Just create a Foreign Key constraint on gis_osm_places referencing index_name.此功能是内置的。只需在 gis_osm_places 上创建一个引用 index_name 的外键约束。

alter table gis_osm_places 
  add constraint osm_places2index_fk 
      foreign key (index_a)
      references index_name(index_ref);

Now drop your trigger and the trigger function.现在放下你的触发器和触发器 function。 The downside being you do not get the message you have created.缺点是您没有收到您创建的消息。 But you handle that in your exception processing in the your app.但是您在应用程序的异常处理中处理它。

I finally found what went wrong !我终于发现出了什么问题!

CREATE FUNCTION public.check_column_value()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF

AS $BODY$

DECLARE
ref_allowed character varying;
BEGIN

IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE'
THEN

IF NEW.index_a is not null AND NEW.index_a NOT IN (SELECT index_ref from public.index_name)
THEN

ref_allowed := (SELECT string_agg(index_ref,',') from public.index_name);
RAISE EXCEPTION 'NOT ALLOWED COMMIT. PLEASE USE : %',ref_allowed;

END IF;
RETURN NEW;

END IF;
END;

$BODY$;

ALTER FUNCTION public.check_column_value()
    OWNER TO "postgres";

CREATE TRIGGER check_column_value
    BEFORE INSERT OR UPDATE
    ON public.gis_osm_places
    FOR EACH ROW
    EXECUTE PROCEDURE public.check_column_value();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM