0

I have a table called base which I need to update after a record has been inserted. I have written a function.

CREATE OR REPLACE FUNCTION host_ip() RETURNS trigger AS $host_ip$
BEGIN

    update  base set thumbnail_url =
    replace ("thumbnail_url",'localhost','myipadd') WHERE id = NEW.id;
    RETURN NEW;
END;
$host_ip$ LANGUAGE plpgsql;

This function is giving me errors when a record is inserted. I have a trigger which calls the function after insert. The error is PL/pgSQL function host_ip() line 4 at SQL statement SQL statement

The trigger is:

CREATE TRIGGER host_ip AFTER INSERT OR UPDATE   ON base
FOR EACH ROW EXECUTE PROCEDURE host_ip();
1
  • I have added the trigger Commented Nov 9, 2016 at 8:26

1 Answer 1

1

To change the column of the just inserted or updated row, you only need to assign the value. There is no need to run an update.

CREATE OR REPLACE FUNCTION host_ip() RETURNS trigger 
AS 
$host_ip$
BEGIN
   new.thumbnail_url := replace(thumbnail_url,'localhost','myipadd');
   RETURN NEW;
END;
$host_ip$ 
LANGUAGE plpgsql;

But you can't change the value in an AFTER trigger, you need a BEFORE trigger for this:

CREATE TRIGGER host_ip 
   BEFORE INSERT OR UPDATE ON base
FOR EACH ROW EXECUTE PROCEDURE host_ip();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.