In the light of DRY, I'm trying to avoid the duplication of insert and update logic in Slick table definitions. I tried this:
trait Entity {
def id: Option[Int]
}
case class BankRekening(id: Option[Int], nummer: String, omschrijving: String) extends Entity{
}
object BankRekeningen extends Table[BankRekening]("bankrekening") with InsertAndUpdate[BankRekening] {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def nummer = column[String]("nummer")
def omschrijving = column[String]("omschrijving")
def * = id.? ~ nummer ~ omschrijving <> (BankRekening, BankRekening.unapply _)
def idx_nummer = index("idx_nummer", nummer, unique = true)
}
trait InsertAndUpdate[T <: Entity] {
this: Table[T] =>
def id: scala.slick.lifted.Column[Int]
def insert(obj: T): T = {
obj.copy(id = Some(this.returning(this.id) insert obj))
}
}
Now the compiler complains about 'obj' in the last statement, saying: could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[T]
Any ideas?