Question

Let's say I want to have an ID columen in all my case classes that I will use with slick:

abstract class BaseEntity(val _id:Option[Long])
case class SomeEntity(id:Option[Long],value:String) extends BaseEntity(id)

Now let's define the abstract class for the scheme and a real scheme:

  abstract class BaseScheme[A  <: BaseEntity](tag:Tag,name:String) extends Table[A](tag,name) {
    def id = column[Long]("ID",O.PrimaryKey,O.AutoInc)
  }
  class SomeEntityScheme(tag:Tag) extends BaseScheme[SomeEntity](tag,"SOME_NAME") {
    def value = column[String]("value",O.NotNull)
    def * = (id.?,value) <> (SomeEntity.tupled,SomeEntity.unapply)
  }

  val someEntitiesTable = TableQuery[SomeEntityScheme]

Now since I thought that I'm bored with writing the AutoIncInsert method I will create a generic one and this is the moment where I fall short:

 object BaseScheme {
     def autoIncInsert[T <: BaseScheme[_]] (t : TableQuery[T]) = t returning t.map(_.id) into {case (p,id) => p.copy(id = Some(id))}
  }

and of course the output of problem:

[error] /home/tomaszk/Projects/4.4/mCloud/application/service-execution/execution-provisioning-model/src/main/scala/pl/mlife/mcloud/provisioning/database/schemes/ExcludingServicesScheme.scala:48: could not find implicit value for evidence parameter of type scala.slick.ast.TypedType[T#TableElementType]
[error]      def remap[T <: BaseScheme[_]] (t : TableQuery[T]) = t returning t.map(_.id) into {case (p,id) => p.copy(id = Some(id))}
[error]                                                                                                       ^
[error] one error found
Was it helpful?

Solution

I may be possible with your approach, I don't have enough knowledge to say it can't, but it sounds a bit fishy and overly complicated. What I did was using the same approach I used here, that is define a method that takes a T#TableElementType (that would be a row object) and a TableQuery object and do some generic operations:

def autoIncInsert[T <: BaseScheme[_]](row: T#TableElementType, tableReference: TableQuery[T])(implicit s: Session) = {
  (tableReference returning tableReference.map(_.id)) += row
}

Then as type T you should pass SomeEntityScheme class.

This should solve your problem in a more coincise and simple way in my opinion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top