试图将头围绕在光滑的3 api上。
我有这些方法签名:
def findById(id: Long): DBIO[Option[Project]] =
Projects.filter(_.id === id).result.headOption
def insert(Task: Task): DBIO[Long] =
Tasks returning Tasks.map(_.id) += Task
我希望在我的控制器中的单个事务中运行。
我的代码目前看起来像这样并且不是交易代码:
def addTaskToProject(taskName: String, projectId: Long) = Action.async { implicit rs =>
val query = for {
Some(project) <- projectDAO.findById(projectId)
id <- taskDAO.insert(Task(0, "blue", project.id))
}yield id
val result = dbConfig.db.run(query)
result.map{ taskId =>
Ok("I have created a new task: " + taskId)
}
}
1)我试过这样做:
val result = dbConfig.db.run(query).transactionally
但这给了我这个错误:
... value transactionally is not a member of scala.concurrent.Future[Long]
2)然后我尝试了这个:
val result = dbConfig.db.run(query.transactionally)
并收到此错误:
... value transactionally is not a member of slick.dbio.DBIOAction[Long,slick.dbio.NoStream,slick.dbio.Effect.All with slick.dbio.Effect.All]
我怎样才能运行这个简单的例子?
答案 0 :(得分:4)
事务性是DBIO的功能, 试试这个:
val result = dbConfig.db.run(query.transactionally)
P.S。实际上存在从DBIOAction
到JdbcActionExtensionMethods
的隐式转换,其具有transactionally
函数