PSQL `returning` statement returning Unit
See original GitHub issueBuild Environment SQLDelight version: 1.5.0 OS: Windows 10 Gradle version: 6.7.1 Kotlin version: 1.4.32 AGP Version (if applicable):
Describe the bug
I have a schema like
CREATE TABLE users
(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL
)
I wrote a query something like this
addUser:
INSERT INTO users(name) VALUES (?) RETURNING id
So the addUser
fun generated by the compiler is still returning Unit
instead of id of type Int
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:8 (1 by maintainers)
Top Results From Across the Web
6.4. Returning Data from Modified Rows - PostgreSQL
In an INSERT , the data available to RETURNING is the row as it was inserted. This is not so useful in trivial...
Read more >Use PostgreSQL `RETURNING` and `WITH` to return updated ...
In the last statement the updated rows are added to the FROM clause, making their columns available for the SELECT and WHERE clauses....
Read more >Postgres INSERT .. RETURNING clause and how this can be ...
A recent request made me think about Postgres' INSERT . ... RETURNING clause, then an additional SELECT statement has to be issued right ......
Read more >How to make INSERT ... RETURNING statement work when ...
I am using Postgres v11. In the code I have units , which is a top level object. units have subunits in a...
Read more >Use RETURNING Clause to Avoid Unnecessary SQL ...
The RETURNING clause allows you to retrieve values of columns (and expressions based on columns) that were modified by an insert, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@pabl0rg I think the issue is in SQLDelight, not sql-psi. From https://github.com/AlecStrong/sql-psi/pull/163
This is a first draft of what the generated code should look like for a mutator statement which has a
RETURNING
clause. There’s a couple of use cases here.In an offline conversation with @AlecStrong we came to the conclusion that
RETURNING
shouldn’t expose aQuery
object. Queries allow the consumer to observe values as they change, which aren’t relevant to single shot mutations that return something. Instead, this will be a synchronous API which just returns raw values.The below is in PostgreSQL dialect because that’s the first dialect we’ll support returning with. SQLite will come after.
RETURNING a single value from a single row
RETURNING multiple values from a single row
Mutating multiple rows
I think this is where things begin to breakdown when not returning some sort of
Query
object. We don’t try to infer what kind of data is returned from a query in other places, and instead leave that up to the consumer of the API with tools likeQuery.executeAsOne()
andQuery.executeAsList()
. It’s possible we do need some type of query inteface which doesn’t allow async interaction.Does the above
deleteAll
return a single value, or many values? It’s hard to say, and from what I understand of SQLDelight, we don’t want to be the entity making that choice.Instead, we could potentially pull some of the interface of
Query
upwards.Query
’s specialization allows observing, and separate implementations ofAbstractQuery
wouldn’t offer observation.