How to get optional result in insert statements with Doobie?

元气小坏坏 提交于 2019-12-10 15:35:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!