1esha
1esha

Reputation: 1722

Using Projection to define list of columns to select in 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.

Upvotes: 3

Views: 1252

Answers (2)

cmbaxter
cmbaxter

Reputation: 35443

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.

Upvotes: 4

stefan.schwetschke
stefan.schwetschke

Reputation: 8932

Just try

...map(ADB.forSelect)

or

for( ... ) yield ADB.forSelect

The following worked for me:

import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession


object ADB extends Table[(Long, String)]("a") {
  def id = column[Long]("id", O.PrimaryKey)
  def name = column[String]("name")
  def * = id ~ name 

  def forSelect = id ~ name
}

Database.forURL("jdbc:h2:mem:test1", driver = "org.h2.Driver") withSession {
  ( for( a <- ADB ) yield ADB.forSelect ).list
}

Upvotes: 0

Related Questions