如何使用 Doobie 在插入语句中获得可选结果?

How to get optional result in insert statements with Doobie?

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

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

运行 此查询具有:

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

失败

Result set exhausted: more rows expected

condition为假

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


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?

正如@Thilo 评论的那样,您可以使用 withGeneratedKeys 返回 Stream[F, Long](其中 F 是您的效果类型)

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

这里是 doc.