Re: how to determine OID of the row I just inserted??? - Mailing list pgsql-general

From Joe Conway
Subject Re: how to determine OID of the row I just inserted???
Date
Msg-id 3E4295E6.8040204@joeconway.com
Whole thread Raw
In response to Re: how to determine OID of the row I just inserted???  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Tom Lane wrote:
> "Jules Alberts" <jules.alberts@arbodienst-limburg.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


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Crash Backend in 7.3.1
Next
From: Bruno Wolff III
Date:
Subject: Re: Pg_dumpall problem