首页 > 解决方案 > PostgreSQL 触发器与 LibreOffice Base 前端

问题描述

我在 PostgreSQL 12.1 中有以下触发函数和触发器:

create or replace function constraint_for_present()
returns trigger
as $$
BEGIN
if
    new.present_status = 'viewing'
    and new.name not in (select viewable_item from sourcing)
then raise exception 'a present_status of "viewing" requires that the viewable item is in sourcing';
end if;
return new;
END;
$$ language plpgsql;

create trigger constraint_for_present
    before insert or update of present_status on viewable_item
    for each row
    execute function constraint_for_present();

这些在 psql 和 TablePlus 客户端中的数据输入期间按预期工作。但是,通过 LibreOffice Base 访问数据库时,该函数会引发错误:

pq_driver: [PGRES_FATAL_ERROR]ERROR:  relation "sourcing" does not exist  
LINE 2:   and new.name not in (select viewable_item from sourcing)  
    
QUERY:  SELECT new.present_status = 'viewing'  
    and new.name not in (select viewable_item from sourcing)  
CONTEXT:  PL/pgSQL function viewing.constraint_for_present() line 3 at IF  
(caused by statement 'UPDATE "viewing"."viewable_item" SET "present_status" = 'none' WHERE "name" = 'test4'')

在 Base 中,我为触发器的表设置了一个简单的表单,每个外键列设置为列表框,列表内容的类型设置为 Sql(也尝试了 Sql [Native])。每个列表的内容是(带有适当的表和主键列):

select name from viewing.cv_present_status order by name

(出于组织政治原因,此数据库目前使用自然键。)Bound 字段设置为 0,即显示的主键列。

所以...... 2个问题:

  1. 为什么这个问题只发生在 Base 中,我该如何解决它(或者至少更好地解决它)?
  2. 由于 Bound 字段似乎只采用一个整数,这实际上是否意味着您不能将列表框用于具有多列主键的表,至少如果有一个显示的列?

标签: postgresqltriggerslibreoffice-base

解决方案


在触发函数中,可以完全限定表

...
   and new.name not in (select viewable_item from viewing.sourcing)
...

推荐阅读