Question

I've created a custom generator to map my database into my app and I'm having an issue. I've customized it to generate the auto increment columns as optional, so I've made this:

val codegen = new scala.slick.model.codegen.SourceCodeGenerator(model) {
  override def Table = new Table(_){
    override def autoIncLastAsOption = true
  }
}

For the tables that has more than 22 columns, it uses HList implementation and not the Scala Tuple. It is generating this:

implicit def GetResultVoicemailRow(implicit e0: GR[String], e1: GR[Short], e2: GR[java.sql.Timestamp], e3: GR[Option[Int]]): GR[VoicemailRow] = GR{
   prs => import prs._
   val positional = <<?[Int] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[Short] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[String] :: <<[java.sql.Timestamp] :: HNil
   import positional._
   r(1) :: r(2) :: r(3) :: r(4) :: r(5) :: r(6) :: r(7) :: r(8) :: r(9) :: r(10) :: r(11) :: r(12) :: r(13) :: r(14) :: r(15) :: r(16) :: r(17) :: r(18) :: r(19) :: r(20) :: r(21) :: r(22) :: r(23) :: r(24) :: r(0) :: HNil // putting AutoInc last
}

This is causing a compilation error. "not found: value r". What can I do to solve this? Thanks.

Was it helpful?

Solution

Looks like a bug in Slick 2.0.1-RC1.

As a workaround customize the code generator with the following fix.

http://slick.typesafe.com/doc/2.0.1-RC1/code-generation.html#customization

class MyCodeGenerator(model: Model) extends SourceCodeGenerator(model: Model){
  protected def mytuple(i: Int) = termName(s"_${i+1}")
  override def Table = new Table(_){
    override def autoIncLastAsOption = true
    override def PlainSqlMapper = new PlainSqlMapper{
      override def code = {
        val positional = compound(columnsPositional.map(c => (if(c.fakeNullable || c.model.nullable)s"<<?[${c.rawType}]"else s"<<[${c.rawType}]")))
        val dependencies = columns.map(_.exposedType).distinct.zipWithIndex.map{ case (t,i) => s"""e$i: GR[$t]"""}.mkString(", ")
        val rearranged = compound(desiredColumnOrder.map(i => if(hlistEnabled) s"r($i)" else mytuple(i)))
        def result(args: String) = if(mappingEnabled) s"$factory($args)" else args
        val body =
          if(autoIncLastAsOption && columns.size > 1){
            s"""
val r = $positional
import r._
${result(rearranged)} // putting AutoInc last
            """.trim
          } else
            result(positional)
        s"""
implicit def ${name}(implicit $dependencies): GR[${TableClass.elementType}] = GR{
  prs => import prs._
  ${indent(body)}
}
        """.trim
      }
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top