简体   繁体   中英

Slick projection <> to a trait with companion object

I'm new to Scala & Slick and am having some trouble implementing user accounts.

User class & companion object:

case class User(id: Long, email: String, password: String, role: Role)

object User {
}

Role trait & companion object:

sealed trait Role

object Role {
    case object Administrator extends Role
    case object NormalUser extends Role
}

UserRepository Schema config

import javax.inject.{ Inject, Singleton }
import play.api.db.slick.DatabaseConfigProvider
import slick.jdbc.JdbcProfile

import scala.concurrent.{ Future, ExecutionContext }

@Singleton
class UserRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
  private val dbConfig = dbConfigProvider.get[JdbcProfile]
  import dbConfig._
  import profile.api._

  private class UsersTable(tag: Tag) extends Table[User](tag, "users") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def email = column[String]("email")
    def password = column[String]("password")
    def role = column[String]("role")

    def * = (id, email, password, role) <> ((User.apply _).tupled, (User.unapply _))
  }

I'm getting a type mismatch on User.apply and User.unapply

[error]  found   : ((Long, String, String, models.Role)) => models.User
[error]  required: ((Long, String, String, String)) => ?
[error]     def * = (id, email, password, role) <> ((User.apply _).tupled, (User.unapply _))

I've tried writing my own function type as suggested in the slick documentation 1 as well as creating a custom scalar type using MappedColumnType 2 but neither seem to work.

Any help would be greatly appreciated!

Mistake is here:

def role = column[String]("role")

There should be Role instead of String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM