Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made? - Mailing list pgsql-general

From electrotype
Subject Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
Date
Msg-id 4ce0f5f5-d608-887b-b5ef-21ae187e3b58@gmail.com
Whole thread Raw
In response to Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?  (Dave Cramer <davecramer@postgres.rocks>)
Responses Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
Re: JDBC driver - is "getGeneratedKeys()" guaranteed to return the ids in the same order a batch insert was made?
List pgsql-general
So I'm curious. Why does order matter ?

Dave Cramer


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.

pgsql-general by date:

Previous
From: Wolfgang Walther
Date:
Subject: Execution order of CTEs / set_config and current_setting in the same query
Next
From: Joe Conway
Date:
Subject: Re: postgres-10 with FIPS