Received: from localhost (uranus.hub.org [200.46.204.60]) by postgresql.org (Postfix) with ESMTP id 9E5419FA12B for ; Mon, 13 Nov 2006 13:24:54 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.60]) (amavisd-new, port 10024) with ESMTP id 07422-03 for ; Mon, 13 Nov 2006 13:24:51 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 794739FA0B9 for ; Mon, 13 Nov 2006 13:24:51 -0400 (AST) Received: (qmail 10975 invoked from network); 13 Nov 2006 17:24:49 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.2?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 13 Nov 2006 17:24:49 -0000 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v752.2) X-Priority: 3 Content-Type: multipart/alternative; boundary=Apple-Mail-82-447894844 Message-Id: Cc: "PostgreSQL JDBC" From: Dave Cramer Subject: Re: Help with transactions and FK's Date: Mon, 13 Nov 2006 12:24:48 -0500 To: Michael Schmidt X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/66 X-Sequence-Number: 16108 --Apple-Mail-82-447894844 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Michael, It will work as you expect. You actually don't even have to get it back, as long as you use the same connection you can do assuming the name of the sequence for parent record is parent_id_seq insert into parent record insert into child (child_id,.....) values (currval ('parent_id_seq'),.....) Dave On 13-Nov-06, at 12:05 PM, Michael Schmidt wrote: > Folks, > I'm sure this is a basic question, but after reading the PG > documentation and some posts on the lists, I still haven't found a > solution. So, I hope someone can provide an answer without too > much effort. > > I have a scientific bibliography database in which a "data item" > consists of a parent record with several child and grandchild > records, linked with foreign keys. Using my Java front-end and > JDBC, I want to insert a new "data item" via a transaction. My > thought is to do the following steps: > > SET TRANSACTION > insert the parent record > get the parent record primary key with currval() of the primary key > sequence > set the child record foreign key value to the value from currval() > insert the child records > COMMIT > roll back on error > > for the grandchild records, several currval() calls will be > needed. My question is - will currval() provide the correct > primary key value for the records inserted in the transaction, or > will it provide the value of the sequence at the beginning of the > transaction? If the latter, how to proceed? > > Thanks for your time. > Michael Schmidt > --Apple-Mail-82-447894844 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 Michael,

It will work as you = expect.

You = actually don't even have to get it back, as long as you use the same = connection you can do=A0

assuming the name of the = sequence for parent record is parent_id_seq=A0

insert into parent = record

insert into child = (child_id,.....) values (currval('parent_id_seq'),.....)

Dave
On = 13-Nov-06, at 12:05 PM, Michael Schmidt wrote:

Folks,
I'm sure this is = a basic question, but after reading the PG documentation and some posts = on the lists, I still haven't found a solution.=A0 So, I hope someone = can provide an answer without too much effort.
=A0
I have a = scientific bibliography database in which a "data item" consists of a = parent record with several child and grandchild records, linked with = foreign keys.=A0 Using my Java front-end and JDBC, I want to insert a = new "data item" via a transaction.=A0 My thought is to do the following = steps:
=A0
SET TRANSACTION
insert the parent = record
get the parent record primary key with = currval()=A0of the primary key sequence
set the child record foreign key value to the value from = currval()
insert=A0the child = records
COMMIT
roll back on error
for the grandchild records, several = currval() calls will be needed.=A0 My question is - will currval() = provide the=A0correct primary key value for the records inserted in the = transaction, or will it provide the value of the sequence at the = beginning of the transaction?=A0 If the latter, how to = proceed?
=A0
Thanks for your time.
Michael = Schmidt


= --Apple-Mail-82-447894844--