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
?
withUniqueGeneratedKeys
expects exactly one row. MaybewithGeneratedKeys
(which returns a Stream of all of them) works better here?