Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 469269FB280 for ; Mon, 13 Nov 2006 14:56:20 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 24906-02-5 for ; Mon, 13 Nov 2006 14:56:07 -0400 (AST) X-Greylist: delayed 01:42:35.035533 by SQLgrey- Received: from bay0-omc2-s15.bay0.hotmail.com (bay0-omc2-s15.bay0.hotmail.com [65.54.246.151]) by postgresql.org (Postfix) with ESMTP id 07F0F9FBBE0 for ; Mon, 13 Nov 2006 14:48:39 -0400 (AST) Received: from hotmail.com ([65.54.169.76]) by bay0-omc2-s15.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.1830); Mon, 13 Nov 2006 10:48:38 -0800 Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Mon, 13 Nov 2006 10:48:38 -0800 Message-ID: Received: from 71.219.233.198 by BAY114-DAV4.phx.gbl with DAV; Mon, 13 Nov 2006 18:48:37 +0000 X-Originating-IP: [71.219.233.198] X-Originating-Email: [michaelmschmidt@msn.com] X-Sender: michaelmschmidt@msn.com From: "Michael Schmidt" To: "Dave Cramer" Cc: "PostgreSQL JDBC" References: Subject: Re: Help with transactions and FK's Date: Mon, 13 Nov 2006 11:48:36 -0700 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0024_01C70719.A7353F50" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: MSN 9 Seal-Send-Time: Mon, 13 Nov 2006 11:48:36 -0700 X-MimeOLE: Produced By MSN MimeOLE V9.20.0029.3000 X-OriginalArrivalTime: 13 Nov 2006 18:48:38.0958 (UTC) FILETIME=[551734E0:01C70754] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.694 tagged_above=0 required=5 tests=AWL, BAYES_20, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, HTML_MESSAGE, MSGID_FROM_MTA_HEADER, SPF_PASS X-Spam-Level: * X-Archive-Number: 200611/67 X-Sequence-Number: 16109 This is a multi-part message in MIME format. ------=_NextPart_000_0024_01C70719.A7353F50 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Mr. Cramer, Thanks for the very quick reply. Given the sophistication of = PostgreSQL, I had hoped it would be that simple! Michael Schmidt ----- Original Message -----=20 From: Dave Cramer=20 To: Michael Schmidt=20 Cc: PostgreSQL JDBC=20 Sent: Monday, November 13, 2006 10:24 AM Subject: Re: [JDBC] Help with transactions and FK's Michael,=20 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=20 assuming the name of the sequence for parent record is parent_id_seq=20 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 ------=_NextPart_000_0024_01C70719.A7353F50 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Mr. Cramer,
Thanks for the very quick reply.  Given the sophistication of=20 PostgreSQL, I had hoped it would be that simple!
Michael Schmidt
----- Original Message -----
To: Michael Schmidt
Cc: PostgreSQL JDBC
Sent: Monday, November 13, 2006 = 10:24=20 AM
Subject: Re: [JDBC] Help with=20 transactions and FK's

Michael,=20

It will work as you expect.

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

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

insert into parent record

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

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

------=_NextPart_000_0024_01C70719.A7353F50--