Re: Trigger not working as expected, first row gets a null value

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: novnov <novnovice(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger not working as expected, first row gets a null value
Date: 2007-08-15 02:06:01
Message-ID: 20366.1187143561@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

novnov <novnovice(at)gmail(dot)com> writes:
> The function is meant to find a value via the internal sql, and then update
> a column in the same tabled that invoked the trigger with that value. Is
> that a non-workable scenario? IE, an after insert trigger can't modify a
> column in the same table that was updated?

Um, if you're trying to do something by modifying the NEW record, that's
correct --- the insertion was already done and you can't change it
retroactively. You could issue a whole new INSERT or UPDATE command,
but beware of creating an infinite loop of trigger firings. It'd be
pretty inefficient anyway since it'd double the work involved for any
change. If you want to modify the row that's going into the table, far
better to do it in a before trigger.

The design rule of thumb is "check or modify the new row in a BEFORE
trigger, propagate the info to other tables in an AFTER trigger".

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ow Mun Heng 2007-08-15 02:06:50 Re: Insert or Replace or \copy (bulkload)
Previous Message Ron Johnson 2007-08-15 01:59:34 Re: MVCC cons