pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pavel Stehule <pavel.stehule@gmail.com>
To: Andreas Joseph Krogh <andreas@visena.com>
Cc: postgres list <pgsql-sql@postgresql.org>
Subject: Re: Update columns in the same table in a deferred constraint trigger
Date: Tue, 29 Jul 2014 11:56:17 +0200
Message-ID: <CAFj8pRBGLHFwp8Gur2VydJ3TmpXG=1yPs_qwMR0bnVf6O2CAuA@mail.gmail.com> (raw)
In-Reply-To: <VisenaEmail.39.362cb9f24c93a770.14781853e94@tc7-on>
References: <VisenaEmail.39.362cb9f24c93a770.14781853e94@tc7-on>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

Hi


2014-07-29 11:52 GMT+02:00 Andreas Joseph Krogh <andreas@visena.com>:

> Hi all.
>
> I have this simple schema:
>
> create table fisk(
>     name varchar primary key,
>     autofisk varchar
> );
>
> I want to update the column "autofisk" on commit based the value of
> "name", so I created this trigger:
>
> CREATE OR REPLACE FUNCTION fisk_tf() returns TRIGGER AS $$
> BEGIN
>     raise notice 'name %', NEW.name;
>     NEW.autofisk = NEW.name || CURRENT_TIMESTAMP::text;
>     RETURN NEW;
> END;
> $$ LANGUAGE plpgsql;
>
> CREATE CONSTRAINT TRIGGER fisk_t AFTER INSERT OR UPDATE ON fisk DEFERRABLE
> INITIALLY DEFERRED
>

It should be BEFORE INSERT OR UPDATE trigger

Regards

Pavel



> FOR EACH ROW EXECUTE PROCEDURE fisk_tf();
>
> The problem is that "autofisk" is never populated:
>  andreak=# begin;
> BEGIN
> andreak=# insert into fisk(name) values ('a');
> INSERT 0 1
> andreak=# commit;
> NOTICE:  name a
> COMMIT
> andreak=# table fisk;
>  name | autofisk
> ------+----------
>  a    |
> (1 row)
>
> Is it possible to do what I want, namely to update a column in a table in
> an AFTER INSERT OR UPDATE constraint trigger on the same table?
>
> Thanks.
>
>  --
> *Andreas Joseph Krogh*
> CTO / Partner - Visena AS
> Mobile: +47 909 56 963
> andreas@visena.com
> www.visena.com
>  <https://www.visena.com;
>

view thread (12+ messages)  latest in thread

Message-ID: <CAFj8pRBGLHFwp8Gur2VydJ3TmpXG=1yPs_qwMR0bnVf6O2CAuA@mail.gmail.com>
Permalink:  ../CAFj8pRBGLHFwp8Gur2VydJ3TmpXG=1yPs_qwMR0bnVf6O2CAuA@mail.gmail.com/
Also on:    postgresql.org/message-id/CAFj8pRBGLHFwp8Gur2VydJ3TmpXG=1yPs_qwMR0bnVf6O2CAuA@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pavel.stehule@gmail.com, andreas@visena.com
  Subject: Re: Update columns in the same table in a deferred constraint trigger
  In-Reply-To: <CAFj8pRBGLHFwp8Gur2VydJ3TmpXG=1yPs_qwMR0bnVf6O2CAuA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox