【发布时间】:2015-08-22 17:53:12
【问题描述】:
如果不存在,我正在尝试插入,我找到了 1.0.1、2.0 的 this post。
我发现 sn-p 在the docs of 3.0.0 中使用事务性
val a = (for {
ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
_ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally
val f: Future[Unit] = db.run(a)
如果这种结构不存在,我正在努力编写插入的逻辑。我是 Slick 的新手,对 Scala 几乎没有经验。如果事务之外不存在,这是我尝试插入...
val result: Future[Boolean] = db.run(products.filter(_.name==="foo").exists.result)
result.map { exists =>
if (!exists) {
products += Product(
None,
productName,
productPrice
)
}
}
但是如何将它放入事务块中?这是我能走的最远:
val a = (for {
exists <- products.filter(_.name==="foo").exists.result
//???
// _ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally
提前致谢
【问题讨论】:
-
如果记录已经存在,您可以重写记录,slick 支持通过
products.insertOrUpdate进行更新插入