user4853187
user4853187

Reputation:

Get last id before transaction commit using postgres in Golang

I have 2 insert queries.

In the second insert query, I need the first insert returned id.

I am doing those queries as a transaction, because they depend each other.

So, is it possible to get the last insert id before committing transaction in postgres in Golang.

Upvotes: 0

Views: 1361

Answers (1)

Andomar
Andomar

Reputation: 238058

Postgres supports the returning keyword to select the inserted id:

INSERT INTO YourTable (col1, col2) VALUES ('Value1', 'Value2') RETURNING id;

Upvotes: 1

Related Questions