Re: On insert duplicate row, return existing key

From: Nick <nboutelier(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: On insert duplicate row, return existing key
Date: 2010-08-04 22:03:39
Message-ID: 523ff3a8-79d4-403b-97c8-8ba512d1c8d8@u4g2000prn.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Anyone? Please

On Jul 31, 12:36 pm, Nick <nboutel(dot)(dot)(dot)(at)gmail(dot)com> wrote:
> If I insert a duplicate row into a table, id like to return the
> existing key.
>
> I tried creating a rule for this...
>
> CREATE RULE no_duplicates AS ON INSERT TO names WHERE EXISTS (SELECT 1
> FROM names WHERE new.name = name) DO INSTEAD SELECT id, name FROM
> names WHERE name = new.name;
>
> However, I get an error on...
>
> INSERT INTO names (name) VALUES ('existing') RETURNING *;
>
> ERROR:  cannot perform INSERT RETURNING on relation "names"
> HINT:  You need an unconditional ON INSERT DO INSTEAD rule with a
> RETURNING clause.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message J. Greg Davidson 2010-08-05 00:14:16 Re: Danger of idiomatic plpgsql loop for merging data
Previous Message Tom Lane 2010-08-04 19:51:47 Re: hashtable issue - HASH_FIND??