Slick/Scala: What is a Rep[Bind] and how do I turn it into a value?

前端 未结 1 1936
时光取名叫无心
时光取名叫无心 2021-01-04 20:34

I\'m trying to figure out Slick (the Scala functional relational model). I\'ve started to build a prototype in Slick 3.0.0 but of course... most of the documentation is eith

相关标签:
1条回答
  • 2021-01-04 21:37

    Rep[] is a replacement to the Column[] datatype used in slick .

    users.map(_.id) returns values of the Column('n_user_id') for all rows

    val result : Rep[Long] = users.map(_.id)
    
    users.map(_.id) // => select n_user_id from app_dat_user_t;
    

    The obtained value is of type Column[Long] [ which is now Rep[Long] ]. You cannot directly print values of the above resultSet as it is not of any scala collection type

    • You can first convert it to some scala collection and then print it as below :

      var idList : List[Long] = List()
      users.map(_.id).forEach(id =>
      idList = idList :+ id
      )
      

      println(idList)** // if you need to print all ids at once

    • else you can simply use :

      users.map(_.id).forEach(id =>
      println(id)
      ) // print for each id
      

    And ,

    val users = TableQuery[UserModel] // => returns Query[UserModel, UserModel#TableElementType, Seq])
    
    val users = for (user <- users) yield user // => returns Query[UserModel, UserModel#TableElementType, Seq])
    

    both mean the same , So you can directly use the former and remove the latter

    0 讨论(0)
提交回复
热议问题