pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp with transactions and FK's
3+ messages / 2 participants
[nested] [flat]
* Help with transactions and FK's
@ 2006-11-13 17:05 Michael Schmidt <michaelmschmidt@msn.com>
2006-11-13 17:24 ` Re: Help with transactions and FK's Dave Cramer <pg@fastcrypt.com>
0 siblings, 1 reply; 3+ messages in thread
From: Michael Schmidt @ 2006-11-13 17:05 UTC (permalink / raw)
To: pgsql-jdbc
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Help with transactions and FK's
2006-11-13 17:05 Help with transactions and FK's Michael Schmidt <michaelmschmidt@msn.com>
@ 2006-11-13 17:24 ` Dave Cramer <pg@fastcrypt.com>
2006-11-13 18:48 ` Re: Help with transactions and FK's Michael Schmidt <michaelmschmidt@msn.com>
0 siblings, 1 reply; 3+ messages in thread
From: Dave Cramer @ 2006-11-13 17:24 UTC (permalink / raw)
To: Michael Schmidt <michaelmschmidt@msn.com>; +Cc: pgsql-jdbc
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
>
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Help with transactions and FK's
2006-11-13 17:05 Help with transactions and FK's Michael Schmidt <michaelmschmidt@msn.com>
2006-11-13 17:24 ` Re: Help with transactions and FK's Dave Cramer <pg@fastcrypt.com>
@ 2006-11-13 18:48 ` Michael Schmidt <michaelmschmidt@msn.com>
0 siblings, 0 replies; 3+ messages in thread
From: Michael Schmidt @ 2006-11-13 18:48 UTC (permalink / raw)
To: Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc
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 -----
From: Dave Cramer<mailto:pg@fastcrypt.com>
To: Michael Schmidt<mailto:michaelmschmidt@msn.com>
Cc: PostgreSQL JDBC<mailto:pgsql-jdbc@postgresql.org>
Sent: Monday, November 13, 2006 10:24 AM
Subject: Re: [JDBC] Help with transactions and FK's
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
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-11-13 18:48 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-11-13 17:05 Help with transactions and FK's Michael Schmidt <michaelmschmidt@msn.com>
2006-11-13 17:24 ` Dave Cramer <pg@fastcrypt.com>
2006-11-13 18:48 ` Michael Schmidt <michaelmschmidt@msn.com>
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