【发布时间】:2019-10-13 23:35:20
【问题描述】:
我有一个可选的插入查询:
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"
运行此查询:
q.update.withUniqueGeneratedKeys[Option[Long]]("id")
失败
结果集已用完:预计会有更多行
那么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?
【问题讨论】:
-
文档中说
withUniqueGeneratedKeys只需要一行。也许withGeneratedKeys(返回所有这些流)在这里效果更好?