Skip site navigation (1) Skip section navigation (2)

tables referenced from insert...returning

From: "Michael Shulman" <shulman(at)mathcamp(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: tables referenced from insert...returning
Date: 2008-06-24 01:19:06
Message-ID: c3f821000806231819x5d884950kbc66ded594f97ee0@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-general
Hi,

What are the rules about what tables/views can be referenced from the
RETURNING clause of an INSERT?

I am particularly puzzled by the following.  Given these definitions:

CREATE TABLE test (id serial primary key, name varchar);
CREATE VIEW tv AS SELECT * FROM test;

This works:

CREATE RULE _update AS ON UPDATE TO tv DO INSTEAD
  UPDATE test SET name = NEW.name WHERE id = OLD.id RETURNING NEW.*;

But this does not:

CREATE RULE _insert AS ON INSERT TO tv DO INSTEAD
  INSERT INTO test (name) VALUES (NEW.name) RETURNING NEW.*;

It gives

ERROR:  invalid reference to FROM-clause entry for table "*NEW*"
LINE 2:   INSERT INTO test (name) VALUES (NEW.name) RETURNING NEW.*;
                                                              ^
HINT:  There is an entry for table "*NEW*", but it cannot be
referenced from this part of the query.

Why is there a difference?

Thanks!
Mike

Responses

pgsql-general by date

Next:From: Tom LaneDate: 2008-06-24 01:46:58
Subject: Re: tables referenced from insert...returning
Previous:From: Ian MeyerDate: 2008-06-23 23:48:38
Subject: Re: Method to detect certain characters in column?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group