我是Slick 3的新手,到目前为止我已经理解db.run是异步调用.返回Future后运行.map或.flatMap.
我的代码中的问题是所有子查询都不起作用(嵌套db.run).
从概念上讲,我没有得到什么?这样的代码是否有效,如下所示?基本上在第一个查询的.map中我根据第一个查询执行一些操作.
我到处都看到有产量的循环,它是唯一的出路吗?我的代码中的问题是否与返回的Future值有关?
val enterprises = TableQuery[Enterprise]
val salaries = TableQuery[Salary]
//Check if entered enterprise exists
val enterpriseQS = enterprises.filter(p => p.name.toUpperCase.trim === salaryItem.enterpriseName.toUpperCase.trim).result
val result=db.run(enterpriseQS.headOption).map(_ match
{
case Some(n) => {
//if an enterprise exists use the ID from enterprise (n.id) when adding a record to salary table
val addSalary1 = salaries += new SalaryRow(0, n.id, salaryItem.worker)
db.run(addSalary1)
}
case None => {
//if an enterprise with salaryItem.enterpriseName doesn't exist, a new enterprise is inserted …
Run Code Online (Sandbox Code Playgroud)