简体   繁体   English

Slick 3.0将Rep(绑定)转换为值

[英]Slick 3.0 convert Rep(Bind) to value

I am trying to join two tables using Slicks 3.0 monadic joins like this: 我正在尝试使用Slicks 3.0 monadic联接来联接两个表,如下所示:

def getInfo(id: Int) = {

  val innerJoin = for {
    a <- tableA
    b <- tableB if a.id === b.s_id
  } yield b.name

  println(innerJoin) // results in Rep(Bind)

  innerJoin.map(println(_)) // results in Rep(Ref @1535257794)
}

Now I want to get the values from the join results. 现在,我想从联接结果中获取值。 I tried a lot to get the values but it seems to be harder then I imagined. 我做了很多尝试来获得这些值,但是似乎比我想象的要难。 I always get some sort of Rep(Bind) back when I try to print the values (see comments above). 当我尝试打印值时,我总是会得到某种形式的Rep(Bind)(请参阅上面的注释)。

There is also this post which addresses the same problem. 也有这篇文章解决了同样的问题。 I tried the suggested solution which looks like this: 我尝试了如下所示的建议解决方案:

innerJoin.map(c => c).forEach(id =>
  println(id)
)

Which doesn't even compile since forEach is not defined in that place. 由于forEach未在该位置定义,因此甚至无法编译。 I don't really know how to proceed here an what to try next. 我真的不知道如何继续进行下一步的操作。 Can someone help me out? 有人可以帮我吗?

Thanks... 谢谢...

val innerJoin in your code is a slick query which can be extended / modified or turned into a DBIOAction . 您的代码中的val innerJoin是一个光滑的查询,可以扩展/修改它或将其转换为DBIOAction An action is something that can be executed on a database which will eventually return your results: 操作是可以在数据库上执行的操作,最终将返回结果:

val action = innerJoin.result
val results: Future[Seq[String]] = db.run(action)

// finally use map to access your result
results.map(s => s.map(name => ...)  // s is of type Seq[String]

I assumed your for comprehension retuns a String . 我假设您的理解力使String调整了。 Therefore the type of val results is Future[Seq[String]] . 因此, val results的类型为Future[Seq[String]]

Also have a look at http://slick.typesafe.com/doc/3.0.0/dbio.html#executing-database-io-actions 也可以看看http://slick.typesafe.com/doc/3.0.0/dbio.html#executing-database-io-actions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM