4

我有一个可选的插入查询:

val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"

使用以下命令运行此查询:

q.update.withUniqueGeneratedKeys[Option[Long]]("id")

失败了

结果集已用尽:预计会有更多行

那么condition是假的。

如何Optional[Long]使用 Doobie 从插入语句中获取结果?


UPD

.withGeneratedKeys[Long]("id")只是Long为了理解

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

如何检查id

4

1 回答 1

1

正如@Thilo 评论的那样,你可以使用withGeneratedKeys它给你一个Stream[F, Long]F你的效果类型在哪里)

val result = q.update.withGeneratedKeys[Long]("id")

这是文档

于 2019-05-28T13:32:35.927 回答