From: terry@ashtonwoodshomes.com
To: 'Kenneth Gonsalves' <lawgon@thenilgiris.com>
To: Postgresql Sql Group (E-mail) <pgsql-sql@postgresql.org>
Subject: Re: next integer in serial key
Date: Thu, 22 Jul 2004 07:48:22 -0400
Message-ID: <019001c46fe1$cb182b20$2766f30a@development.greatgulfhomes.com> (raw)
In-Reply-To: <0407220943130Q.01150@thenilgiris.com>
Actually it does work, call nextval to get your next value, then call your INSERT statement,
explicitly giving said value for the serial column. Then you can proceed with using said value in
the INSERT statement of the related inserts with foreign keys to it.
Alternatively, you can do:
INSERT (accepting the default)
then SELECT currval(the_sequence_object);
then <extra inserts of related foreign key records>
NOTE: 2nd method assumes that nobody else called nextval() on the sequence between when you did the
insert and when you did the select currval(). Note that being inside a transaction is NOT
sufficient, you need an explicit lock on the sequence. I do not recommend the 2nd method, too much
can go wrong.
Terry Fielder
Manager Software Development and Deployment
Great Gulf Homes / Ashton Woods Homes
terry@greatgulfhomes.com
Fax: (416) 441-9085
> -----Original Message-----
> From: Kenneth Gonsalves [mailto:lawgon@thenilgiris.com]
> Sent: Thursday, July 22, 2004 12:13 AM
> To: terry@greatgulfhomes.com
> Subject: Re: [SQL] next integer in serial key
>
>
> On Thursday 22 July 2004 10:25 am, you wrote:
> > The same way the default value is defined, which you can
> find by doing:
> > \d tablename
> >
> > Which usually gives something like:
> > Table
> "public.gbs_floorplans"
> > Column | Type |
>
> > Modifiers
> >
> >
> ----------------------+---------------+-----------------------
> -------------
> >----- ---------------------------------
> > floorplan_id | integer | not null default
> > nextval('public.gbs_floorplans_floorplan_id_seq'::text)
> > division_id | character(3) | not null
> > floorplan_display_id | character(10) | not null
> >
> > Hence
> > SELECT nextval('public.gbs_floorplans_floorplan_id_seq'::text)
>
> nope. what happens is that i enter data into a table with a
> serial type id,
> and then use that id as a foreign key to enter data into
> another table. so i
> need to know the id for the second entry. i commit after both entries
> succeed. If i use nextval to find the id, this increments the
> id, which will
> defeat the purpose.
> --
> regards
> kg
>
> http://www.onlineindianhotels.net - hotel bookings
> reservations in over 4600
> hotels in India
> http://www.ootygolfclub.org
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: terry@ashtonwoodshomes.com, lawgon@thenilgiris.com
Subject: Re: next integer in serial key
In-Reply-To: <019001c46fe1$cb182b20$2766f30a@development.greatgulfhomes.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox