首页 > 解决方案 > 当更新整数的类似触发器工作正常时,更新布尔值的触发器不起作用?

问题描述

我有 2 个表,lowlevel_table 和 toplevel_table。基本上低级行包含在顶级行中

低级表

id integer 
status integer
flag boolean 
geom geometry point

顶层表

id integer 
status integer
flag boolean 
geom geometry polygon

当用户更新顶级表上一行的状态值或标志为 false 时,需要将该值传递给其中包含的所有 lowlevel_table 行。目前状态一工作正常,但即使标志一的编码非常相似,它也不起作用

工作状态触发及功能

create trigger status_update before
update of status on
toplevel_table for each row
when  (new.status > old.status) execute function status_update()

CREATE OR REPLACE FUNCTION status_update()
RETURNS trigger
 LANGUAGE plpgsql
AS $function$

 BEGIN  
 if new.status > old.status then 
 update lowlevel_table a set status = new.status where a.status < new.status  
 and st_contains(a.geom, st_centroid(new.geom) );
 end if;
 RETURN NEW;                                                                    
 END;
    
 $function$
 ;

不工作的触发器和功能

create trigger flag_update before
update of flag on
toplevel_table for each row
when  (new.flag is false) execute function flag_update()

CREATE OR REPLACE FUNCTION flag_update()
RETURNS trigger
 LANGUAGE plpgsql
AS $function$

 BEGIN  
 if new.flag is false then 
 update lowlevel_table a set flag = new.flag where a.flag is true 
 and st_contains(a.geom, st_centroid(new.geom) );
 end if;
 RETURN NEW;                                                                    
 END;
    
 $function$
 ;

更新查询

 update toplevel_table set status = 5 where id = myid
 update toplevel_table set flag = false where id = myid

我真的不明白为什么我的布尔触发器在以确切方式编写时不起作用。如何让它发挥作用?

标签: sqlpostgresqlfunctiontriggersboolean

解决方案


推荐阅读