From: electrotype <electrotype@gmail.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
Date: Wed, 9 Dec 2020 09:48:42 -0500
Message-ID: <4ce0f5f5-d608-887b-b5ef-21ae187e3b58@gmail.com> (raw)
In-Reply-To: <CADK3HH+h60g5dmZq4qhESo1Q1yn-x4EGixYZbOGufc=tHFu-1A@mail.gmail.com>
References: <36689061-0844-2dfd-a4ee-8006d8a15ca9@gmail.com>
<CADK3HHL68fd+D_e-jGdx9J--hrwEx26-5a-V6fFf=OOLUTgApw@mail.gmail.com>
<1d0c8b21-25cc-2a3e-7f25-81f2de645f1e@gmail.com>
<CADK3HH+h60g5dmZq4qhESo1Q1yn-x4EGixYZbOGufc=tHFu-1A@mail.gmail.com>
> So I'm curious. Why does order matter ?
>
> Dave Cramer
> www.postgres.rocks
When you have to save multiple new entities with subentities.
You first save all the parent entities in a single SQL batch insert, you get the generated ids, then
insert all the subentities in another single SQL batch insert. To know which "parent id" to use for
a given subentity of the second query, you need a way to associate a generated id with the correct
parent entity. The order of the parents in their batch, and the order of the generated ids, is the
only straighforward way.
I know all this could be made into a single SQL query, without having to associate the generated ids
to the parents manually. But sometimes you have to fight /really /hard agains your framework or JDBC
itself to write such more complex query, where two batch inserts are very natural.
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: electrotype@gmail.com
Subject: Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
In-Reply-To: <4ce0f5f5-d608-887b-b5ef-21ae187e3b58@gmail.com>
References: <36689061-0844-2dfd-a4ee-8006d8a15ca9@gmail.com> <CADK3HHL68fd+D_e-jGdx9J--hrwEx26-5a-V6fFf=OOLUTgApw@mail.gmail.com> <1d0c8b21-25cc-2a3e-7f25-81f2de645f1e@gmail.com> <CADK3HH+h60g5dmZq4qhESo1Q1yn-x4EGixYZbOGufc=tHFu-1A@mail.gmail.com> <4ce0f5f5-d608-887b-b5ef-21ae187e3b58@gmail.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