Search code examples
scalaprojectionslick

Scala projections in Slick for only one column


I'm following the Slick documentation example for autoincrementing fields and I'm having trouble creating a mapped projection that ... well, only has one column.

case class UserRole(id: Option[Int], role: String)

object UserRoles extends Table[UserRole]("userRole") {
  def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
  def role = column[String]("ROLE")
  // ...
  def * = id.? ~ role <> (UserRole, UserRole.unapply _)
      // NEXT LINE ERRORS OUT
  def forInsert = role <> ({t => UserRole(None, t._1)}, {(r: UserRole) => Some((r.role))}) returning id   
}

The error is "value <> is not a member of scala.slick.lifted.Column[String]"

I also thought it'd be more efficient to design my schema like so:

case class UserRole(role: String)

object UserRoles extends Table[UserRole]("userRole") {
  def role = column[Int]("ROLE", O.PrimaryKey)
  // ...
  def * = role <> (UserRole, UserRole.unapply _)

}

But then I start getting the same error as above, too. "value <> is not a member of scala.slick.lifted.Column[String]"

What am I really doing? Do I just not have a projection anymore because I only have one column? If so, what should I be doing?


Solution

  • This is a known issue with Slick; mapped projections do not work with a single column. See https://github.com/slick/slick/issues/40

    Luckily, you don't need a mapped projection for your code to work. Just omit everything after and including the <>. See scala slick method I can not understand so far for a great explanation of projections. It includes the information you need to get going.