0

Probably something dumb, but I just can't make it work. I have this table:

                                      Table "public.office"
   Column    |           Type           | Collation | Nullable |             Default
-------------+--------------------------+-----------+----------+-------------------------------
 id          | integer                  |           | not null | generated by default as identity
 name        | text                     |           | not null |
 url         | text                     |           |          |
 domain      | text                     |           |          |

...
Triggers:
    updated_save_domain_from_url AFTER UPDATE OF url ON office FOR EACH ROW EXECUTE PROCEDURE save_domain_from_url()

And this trigger function:

-- TRIGGER FUNCTION
CREATE OR REPLACE FUNCTION public.save_domain_from_url()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
  new.domain := substring(new.url from '(?:.*://)?(?:www\.)?([^/]*)');
  RETURN new;
END;
$function$

Example row:

 id  |              name               |  url   | domain
-----+---------------------------------+--------+--------
 425 | Van Halewyck & Marco Architects | [NULL] | [NULL]

When I update the URL:

update office set url = 'http://vanhalewyck-marco.com/en' where id = 425;

The domain is still null, trigger did not work:

id  |              name               |               url               | domain
-----+---------------------------------+---------------------------------+--------
 425 | Van Halewyck & Marco Architects | http://vanhalewyck-marco.com/en | [NULL]

Any hints of what might be going on, please help!!

Thanks

1 Answer 1

1

You need to have the trigger defined as BEFORE trigger. AFTER triggers (as yours) cannot alter the new record as they are executed after the record is inserted.
Best regards,
Bjarni

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.