Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can i use RETURNING in INSERT? #83

Open
bigBron opened this issue Sep 5, 2022 · 6 comments
Open

Can i use RETURNING in INSERT? #83

bigBron opened this issue Sep 5, 2022 · 6 comments

Comments

@bigBron
Copy link

bigBron commented Sep 5, 2022

Using the 'RETURNING' syntax to get incremented ids in PostgresQL, how does it work in go-sqlbuilder

INSERT INTO table (field) VALUES (a) RETURNING id;
@huandu
Copy link
Owner

huandu commented Sep 6, 2022

It's recommended to build the SQL like this.

ib := sqlbuilder.PostgreSQL.NewInsertBuilder()
ib.InsertInto("table").Cols("field").Values("some value")

// Use Build to add "RETURNING id" as a postfix.
sql, args  := sqlbuilder.Build("$? RETURNING id;", ib)

println(sql)
println(args)

// Output:
// INSERT INTO table (field) VALUES (?) RETURNING id;
// [some value]

@huandu huandu added the question label Sep 6, 2022
@bigBron bigBron closed this as completed Sep 8, 2022
@KyleSanderson
Copy link

It's recommended to build the SQL like this.

ib := sqlbuilder.PostgreSQL.NewInsertBuilder()
ib.InsertInto("table").Cols("field").Values("some value")

// Use Build to add "RETURNING id" as a postfix.
sql, args  := sqlbuilder.Build("$? RETURNING id;", ib)

println(sql)
println(args)

// Output:
// INSERT INTO table (field) VALUES (?) RETURNING id;
// [some value]

Is this still the best way to do this? I'd like to use as many builders as I can without writing my own SQL.

@huandu
Copy link
Owner

huandu commented Jan 6, 2025

Is this still the best way to do this? I'd like to use as many builders as I can without writing my own SQL.

A (maybe) better way is to use SQL() method like this.

ib.InsertInto("table").Cols("field").Values("some value")

// Add the RETURNING expression at the end of the INSERT statement.
ib.SQL("RETURNING id")

println(ib) // OUTPUT: INSERT INTO table (field) VALUES (?) RETURNING id

As RETURNING id is a PostgreSQL specific syntax and there is no widely accepted de-facto way to "return" last insert id among major DBMS, I don't think it's feasible to add a method to InsertBuilder for such feature.

@KyleSanderson
Copy link

Is this still the best way to do this? I'd like to use as many builders as I can without writing my own SQL.

A (maybe) better way is to use SQL() method like this.

ib.InsertInto("table").Cols("field").Values("some value")

// Add the RETURNING expression at the end of the INSERT statement.
ib.SQL("RETURNING id")

println(ib) // OUTPUT: INSERT INTO table (field) VALUES (?) RETURNING id

As RETURNING id is a PostgreSQL specific syntax and there is no widely accepted de-facto way to "return" last insert id among major DBMS, I don't think it's feasible to add a method to InsertBuilder for such feature.

It's built-in to sqlite and a couple others. On subqueries within a transaction it's absolutely invaluable.

@vanyauhalin
Copy link

Hello everyone. I am using SQLite and was also looking for this feature. I would be happy to see it as a builder method.

The RETURNING syntax has been supported by SQLite since version 3.35.0 (2021-03-12).

SQLite Reference

@huandu huandu reopened this Jan 19, 2025
@huandu
Copy link
Owner

huandu commented Jan 19, 2025

Since everyone here is interested in this feature, let me consider how to design it well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants