[clug] OT: Postgresql serial primary keys
Brett Worth
brett.worth at gmail.com
Tue Apr 5 11:40:36 UTC 2022
I've answered my own question.
If anyone's interested, the solution is to use the "returning" keyword of the "insert"
statement. Then you just treat the "insert" like a "select" which will return the updated
value of the serial primary key.
e.g.
|INSERT INTO table_name(column1, column2, …) VALUES (value1, value2, …) RETURNING id;|
Brett
On 5/4/22 15:51, Brett Worth wrote:
> Hey all. I've googled my heart out but do not have an answer to my question. I know
> there are some knowledgeable people on this list so here goes:
>
> I have a database with two tables. The first table has a column which is a "serial
> primary key".
>
> While inside a transaction, I want to insert a row into the first table then use the
> generated key as a value for inserts into the second table.
>
> If I select the max() value from the first table's key column, I cannot see the newly
> generated value. I suspect this is because I have not done a commit yet.
>
> Does anyone know if there's a magic way to see that value?
>
> I'm using postgresql 10.17.
>
> Brett
More information about the linux
mailing list