sqldelight icon indicating copy to clipboard operation
sqldelight copied to clipboard

Adding `RETURNING` to an `INSERT` causes existing usages to silently break

Open JakeWharton opened this issue 2 years ago • 5 comments

Given:

insert:
INSERT INTO People(name)
VALUES (?)
;

and usage as:

val peopleQueries = ...
peopleQueries.insert("Alec")

Change the query to:

 insert:
 INSERT INTO People(name)
 VALUES (?)
+RETURNING id
 ;

and now your call to insert still compiles but critically does not execute.

This is a foot gun waiting to happen. Ideally we either fail all compilation sites somehow (@CheckResult?) or... something else that I can't think of at this time.

JakeWharton avatar May 15 '22 03:05 JakeWharton

(I don't actually think @CheckResult is a good enough solution)

JakeWharton avatar May 15 '22 03:05 JakeWharton

We can probably do this in the IDE by finding usages and highlighting red if the result isn't used

AlecKazakova avatar May 16 '22 14:05 AlecKazakova

I'm wondering if we could still make it run synchronously but instead require a lambda which returns R that ultimately becomes the insert return value.

So

playerQueries.insert("Alec")

before and now after:

val id = playerQueries.insert("Alec") { executeAsOne() }

Since the lambda is required, it would fail to compile on the query change.

JakeWharton avatar May 17 '22 00:05 JakeWharton

maybe, I'm not a huge fan of treating RETURNING as any different from a normal select in the compiler since as is its a super simple implementation

AlecKazakova avatar May 17 '22 00:05 AlecKazakova

Yeah I meant this more as a general strategy for one-shot, non-observable queries.

JakeWharton avatar May 17 '22 01:05 JakeWharton

Maybe related #https://github.com/cashapp/sqldelight/issues/3599

What about adding a query method works like Query but returns R?

hfhbd avatar Oct 19 '22 16:10 hfhbd

Might be a separate issue entirely, but it seems adding a RETURNING clause to an insert prevents a Flow<> from emitting values.

Loggie avatar Oct 20 '22 20:10 Loggie

Might be a separate issue entirely, but it seems adding a RETURNING clause to an insert prevents a Flow<> from emitting values.

This happens for me as well using version 2.0.0-alpha04. Any update on this? @JakeWharton

kansson avatar Jan 16 '23 18:01 kansson

No

JakeWharton avatar Jan 16 '23 18:01 JakeWharton

How to use RETURNING in 2.0.0 @AlecKazakova

JakeWoki avatar Aug 08 '23 02:08 JakeWoki