Re: OID Usage - Mailing list pgsql-general

From Michael Fuhr
Subject Re: OID Usage
Date
Msg-id 20050114101508.GA98256@winnie.fuhr.org
Whole thread Raw
In response to Re: OID Usage  (Christian Kratzer <ck-lists@cksoft.de>)
Responses Re: OID Usage
List pgsql-general
On Fri, Jan 14, 2005 at 10:32:18AM +0100, Christian Kratzer wrote:

> $query = sprintf("SELECT currval('%s_%s_seq') AS
> id",$this->table,$this->id_column);

PostgreSQL 8.0 will have a pg_get_serial_sequence() function that
returns the sequence name for a particular column so you don't have
to construct it.  This is useful when a table or column has been
renamed, in which case the above will probably break.

CREATE TABLE foo (fooid serial);
ALTER TABLE foo RENAME TO bar;
ALTER TABLE bar RENAME fooid TO barid;
\d bar
                            Table "public.bar"
 Column |  Type   |                       Modifiers
--------+---------+--------------------------------------------------------
 barid  | integer | not null default nextval('public.foo_fooid_seq'::text)

SELECT pg_get_serial_sequence('bar', 'barid');
 pg_get_serial_sequence
------------------------
 public.foo_fooid_seq
(1 row)

INSERT INTO bar VALUES (DEFAULT);
SELECT currval(pg_get_serial_sequence('bar', 'barid'));
 currval
---------
       1
(1 row)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-general by date:

Previous
From: Christian Kratzer
Date:
Subject: Re: OID Usage
Next
From: Bo Lorentsen
Date:
Subject: Re: OID Usage