Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Projection to define list of columns to select in Slick

Tags:

scala

slick

I have table definition in Slick:

object ADB {
  extends BaseDB[A]("a")
  with PostgresDriver{
  def id = column[Long]("id", O.PrimaryKey)
  def name = column[String]("name")
  ...
  def * = id ~ name ~ ... <> (A.apply _, A.unapply _)

  def forSelect = id ~ name
}

Is it possible to refer to forSelect when querying for A?

I want to keep the list of field to be selected in one place to be able to push forSelect to trait in future.

like image 740
1esha Avatar asked Dec 05 '25 15:12

1esha


1 Answers

I believe you can accomplish what you want like this:

( for( a <- ADB ) yield a.forSelect ).list

The difference between this and what stefan.schwetschke posted is that I'm using the instance a to reference forSelect instead of accessing it from the ADB object itself.

like image 67
cmbaxter Avatar answered Dec 08 '25 22:12

cmbaxter