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

I am trying to figure out Slick (Scala functional relational model). I started creating a prototype in Slick 3.0.0, but of course ... most of the documentation is either outdated or incomplete.

I managed to get to the point where I can create the schema and return the object from the database.

The problem is that I am returning - this is "Rep [Bind]" and not the object I would expect to receive. I can’t understand what to do for this. For example, if I try something like rep.countDistinct.result, I get a crash.

Here is a quick overview of the code ... some are removed for brevity:

class UserModel(tag: Tag) extends Table[User](tag, "app_dat_user_t") { def id = column[Long]("n_user_id", O.PrimaryKey) def created = column[Long]("d_timestamp_created") def * = (id.?, created) <> (User.tupled, User.unapply) } case class User(id: Option[Long], created: Long) val users = TableQuery[UserModel] (users.schema).create db.run(users += User(Option(1), 2)) println("ID is ... " + users.map(_.id)) // prints "Rep[Bind]"... huh? val users = for (user <- users) yield user println(users.map(_.id).toString) // Also prints "Rep[Bind]"... 

I cannot find a way to β€œexpand” the Rep object, and I cannot find a clear explanation of what it is or how to use it.

+6
source share
1 answer

Rep [] replaces the column type [] used in slick.

users.map (_. id) returns column values ​​( '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 resulting value is of type Column [Long] [which is now Rep [Long] ]. You cannot directly print values ​​above resultSet as this is not some type of scala collection

  • You can convert it to some scala collection first 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 identifiers immediately

  • otherwise you can just 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 first and delete the last

+7
source

Source: https://habr.com/ru/post/986846/


All Articles