Re: how to determine OID of the row I just inserted???

From: Joe Conway <mail(at)joeconway(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Jules Alberts <jules(dot)alberts(at)arbodienst-limburg(dot)nl>, pgsql-general(at)postgresql(dot)org
Subject: Re: how to determine OID of the row I just inserted???
Date: 2003-02-06 17:05:42
Message-ID: 3E4295E6.8040204@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom Lane wrote:
> "Jules Alberts" <jules(dot)alberts(at)arbodienst-limburg(dot)nl> writes:
>>Something like lastval() IMHO is way too risky.
>
> currval() is what you want, and it is *not* risky. Read the sequence
> documentation.

Tom's absolutely correct, of course. See:
http://www.us.postgresql.org/users-lounge/docs/7.3/postgres/functions-sequence.html

Another common solution I've used is something like:

create table person(pid serial, name text, aid int);
create table address(aid int, street text);
create sequence address_seq;

Then in PHP (or whatever) do:
select nextval('address_seq');
and put the value into a variable (let's say you get back 42). Now you can do:

insert into person (name, aid) values ('John Doe', 42);
insert into address values (42, 'Penny Lane');

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ken Guest 2003-02-06 17:18:06 how do I get a log of SQL executed by server?
Previous Message Tom Lane 2003-02-06 17:05:25 Re: Crash Backend in 7.3.1