Re: NEW variable values in actions in rules

From: "Ken Winter" <ken(at)sunward(dot)org>
To: "'Tom Lane'" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "'PostgreSQL pg-general List'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: NEW variable values in actions in rules
Date: 2006-02-01 22:31:22
Message-ID: 002e01c6277f$3b4b7a90$6603a8c0@kenxp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom ~

Your answers (below) to my questions about the values of NEW columns in
trigger functions was very clarifying for me (as well as being correct!).
Now I'm having similar problems with NEW values in actions that are embedded
in rules, and the answers don't seem to be the same.

I have a table "person_h" with a not-null column "effective_date_and_time"
that defaults to CURRENT_TIMESTAMP.

I have a view "person" with the following rule defined on it:

CREATE RULE on_insert AS
ON INSERT TO person
DO INSTEAD (
INSERT INTO person_i (person_id, birth_date)
VALUES (nextval('pop_seq'::text), new.birth_date);
INSERT INTO person_h (person_id, ...
effective_date_and_time, ...)
VALUES (currval('pop_seq'::text), last_name, ...
new.effective_date_and_time, new.last_name, ...);

where the "..."s are some other columns not of interest here.

So now when I do this query:

insert into public.person (last_name) values ('Jones');

I get this error:

PostgreSQL Error Code: (1)
ERROR: null value in column "effective_date_and_time" violates not-null
constraint--0 Rows Affected

So the query processor seems not to be behaving as you described in
answering my question 2 below: it is not taking the
NEW.effective_date_and_time value to be "whatever the default is for the
column".

If I submit the same "INSERT INTO person_h..." query directly, rather than
as part of the rule, it works fine.

Help?

~ TIA
~ Ken

> -----Original Message-----
> From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
> Sent: Thursday, January 05, 2006 7:49 PM
> To: Ken Winter
> Cc: PostgreSQL pg-general List
> Subject: Re: [GENERAL] NEW variable values in trigger functions
>
> "Ken Winter" <ken(at)sunward(dot)org> writes:
> > 1. What is the value of the NEW variable for a column that is not
> mentioned
> > in an UPDATE statement? Is it NULL? If not NULL, what?
>
> No, it's whatever value is going to be assigned to the new row (which in
> this particular case would be the same as the OLD value).
>
> > 2. Same questions re the value of a NEW variable that is not assigned a
> > value in an INSERT statement.
>
> Same answer: whatever value is due to go into the row (in this case,
> whatever the default is for the column).
>
> > 3. If an UPDATE query set a column to DEFAULT, what value does a trigger
> > function see for the column's NEW variable?
>
> Same answer.
>
> regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michelle Konzack 2006-02-01 23:06:52 Re: Alternative to knoda, kexi and rekall?
Previous Message rlee0001 2006-02-01 21:55:28 Re: 8.0.3 regexp_replace()...