问题
I have an optional insert query:
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"
Running this query with:
q.update.withUniqueGeneratedKeys[Option[Long]]("id")
fails with
Result set exhausted: more rows expected
then condition
is false.
How to get Optional[Long]
result from insert statements with Doobie?
UPD
.withGeneratedKeys[Long]("id")
gives just Long
in for comprehension
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"
for {
id <- q.update.withGeneratedKeys[Long]("id") // id is long
_ <- if (<id is present>) <some other inserts> else <nothing>
} yield id
How to check id
?
回答1:
As @Thilo commented, you can use the use withGeneratedKeys
which gives you back a Stream[F, Long]
(where F
is your effect type)
val result = q.update.withGeneratedKeys[Long]("id")
Here is the doc.
来源:https://stackoverflow.com/questions/56342218/how-to-get-optional-result-in-insert-statements-with-doobie