简体   繁体   English

Postgresql 触发器在插入时未按预期工作

[英]Postgresql trigger not working as expected at insert

I have created 2 triggers in postgresql that execute the same function.我在 postgresql 中创建了 2 个触发器,它们执行相同的 function。 One for insert and the other one for update.一个用于插入,另一个用于更新。 The update trigger seems to be working fine, but the insert trigger is not working the way I would expect it to.更新触发器似乎工作正常,但插入触发器没有按我期望的方式工作。 I have tried changing the trigger from "before insert" to "after insert" but I didn't seem to make it work.我尝试将触发器从“插入前”更改为“插入后”,但我似乎没有让它工作。

I should be able to set a value for "name_ip" only if column "type" is of a certain value.只有当列“type”具有某个值时,我才应该能够为“name_ip”设置一个值。 With the code I have written, the name_ip column gets set to null, regardless of the type column.使用我编写的代码,name_ip 列设置为 null,无论类型列如何。 Pretty sure it has something to do with address.id = new.id in the where clause.很确定它与 where 子句中的 address.id = new.id 有关。

This is the function I am triggering这是我正在触发的 function

CREATE OR REPLACE FUNCTION update_name_ip_column() 
RETURNS TRIGGER AS $$
BEGIN
    new.name_ip = new.name_ip from address, "record-type" rt 
    where 
        new."type" = rt.id  and
        rt."type" = 'PTR' and
        address.id = new.id;
    RETURN new;
END;
$$ language 'plpgsql';

And these are the 2 triggers I defined这些是我定义的 2 个触发器

drop trigger update_address_name_ip on "address";
CREATE TRIGGER update_address_name_ip before update ON "address"  FOR EACH ROW EXECUTE PROCEDURE  update_name_ip_column();

drop trigger insert_address_name_ip on "address";
CREATE TRIGGER insert_address_name_ip before insert ON "address"  FOR EACH row execute function update_name_ip_column();

As I thought, the problem was using "before insert" in the where clause instead of "after insert", so everything was set to null regardless of input.正如我所想,问题是在 where 子句中使用“插入前”而不是“插入后”,因此无论输入如何,所有内容都设置为 null。

So now I'm doing an update after i do an insert, setting to null everything that doesn't contain 'PTR' in type所以现在我在插入后进行更新,将所有不包含“PTR”的内容设置为 null

--insert  function
CREATE OR REPLACE FUNCTION insert_name_ip_column() 
RETURNS TRIGGER AS $$
begin
    update public.address 
    set name_ip = null
    from "record-type" rt 
    where 
        public.address."type" = rt.id  and not
        rt."type" = 'PTR' and
        public.address.id = new.id;

    --debug Table
    INSERT INTO public.test_table
        (id, name_ip, "type")
        VALUES(new.id, new.name_ip, new.type);

    RETURN new;
END;
$$ language 'plpgsql';
drop trigger insert_address_name_ip on "address";
CREATE TRIGGER insert_address_name_ip after insert ON "address"  FOR EACH row execute function insert_name_ip_column();

You can do is as before doing something like:你可以像before一样做类似的事情:

CREATE OR REPLACE FUNCTION update_name_ip_column() 
RETURNS TRIGGER AS $$
DECLARE
    ptr_val varchar;
BEGIN
    select 
       into ptr_val rt."type" 
    from 
       "record-type" rt 
    where rt.id = new."type";

    if ptr_val != 'PTR'
        new.name_ip = NULL;

    --debug Table
    INSERT INTO public.test_table
        (id, name_ip, "type")
        VALUES(new.id, new.name_ip, new.type);
    RETURN new;
END;
$$ language 'plpgsql';

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

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