Postgres触发器仅在NEW.column = true时才执行过程 [英] Postgres trigger to only execute procedure when NEW.column = true

查看:55
本文介绍了Postgres触发器仅在NEW.column = true时才执行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PostgreSQL-我正在尝试触发仅在 NEW (插入或更新)行的 is_default 列值设置为布尔值为真.

Using PostgreSQL - I'm trying to fire a procedure that runs a trigger only when the NEW (inserted or updated) row has the is_default column value set to boolean true.

这是我尝试过的方法,但是在 WHEN 行上出现语法错误:

This is what I've tried but I get a syntax error on my WHEN line:

CREATE TRIGGER trigger_price_group_default_price_handler
    BEFORE UPDATE OR INSERT
    ON price_groups
    WHEN (NEW.is_default = true)
    EXECUTE PROCEDURE clear_default_price_group();

有什么想法吗?

推荐答案

看似正确的代码中似乎莫名其妙的语法错误的最可能原因是您使用的服务器版本不了解该语法.

The most likely reason for apparently inexpliciable syntax errors in what looks like correct code is that the server version you're using doesn't understand the syntax.

例如,条件触发器是在PostgreSQL 9.0中引入的,并且在8.4或更早版本中不存在,因此8.4或更高版本较旧的服务器不知道 WHEN ... 在那里的含义.

For example, conditional triggers were introduced in PostgreSQL 9.0, and are not present in 8.4 or older so an 8.4 or older server will not have any idea what WHEN ... means there.

我试图在pgsql-docs邮件列表中提出一个论点,我们应该在功能描述旁边加入"Since [version]" ,但是每个人似乎都更喜欢引导人们阅读其版本的文档.所以我还没走.不同于太多的项目( cough Hibernate cough ),至少PostgreSQL具有到其他版本文档的快速链接.

I've tried to make the argument on the pgsql-docs mailing list that we should include "Since [version]" alongside feature descriptions, but everyone just seems to prefer to direct people to read the docs for their version. So I haven't got far. At least PostgreSQL has quick links to other versions' docs, unlike too many projects (cough Hibernate cough).

(这也意味着您使用的是旧的不受支持的PostgreSQL版本,应该升级).

(This also means that you're on an old and unsupported PostgreSQL version and should upgrade).

这篇关于Postgres触发器仅在NEW.column = true时才执行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆