在Slick 3.1中声明日期/时间列会引发编译错误

时间:2018-02-23 22:08:00

标签: scala slick slick-3.0

我有以下代码,包括案例类(MyEmail),相关的Slick类(MyEmailDB)和用于转换LocalDateTime的隐式函数。问题是def * =函数抛出了编译错误。如果我删除程序编译的一个隐式函数,但是如何解决这个问题?

case class MyEmail (sk: Int, subject: String, message: String, 
                    createTS: LocalDateTime, updateTS: Option[LocalDateTime])

class MyEmailDB(tag: Tag) extends Table[MyEmail](tag, "emails") {

    def sk = column[Int]("sk", O.PrimaryKey, O.AutoInc)
    def subject = column[String]("subject")
    def message = column[String]("message")
    def createTS = column[LocalDateTime]("create_ts")(localDateTimeColumnType)
    def updateTS = column[Option[LocalDateTime]]("update_ts")(localDateTimeColumnType2)

    def * = (sk, subject, message, createTS, updateTS) <> ((MyEmail.apply _).tupled, MyEmail.unapply)  // <-- this line throws the compilation error

    implicit val localDateTimeColumnType = MappedColumnType.base[LocalDateTime, Timestamp](
     {
        case localDateTime => Timestamp.valueOf(localDateTime)
     },{
        ts => ts.toLocalDateTime
     }
   )

   implicit val localDateTimeColumnType2 = MappedColumnType.base[Option[LocalDateTime], Timestamp](
     {
        case Some(localDateTime) => Timestamp.valueOf(localDateTime)
        case None => null
     },{
        ts => if (ts != null) Some(ts.toLocalDateTime) else None
     }
   )   
}

错误是:

  

◾发现隐式转换:⇒anyToToShapedValue():   slick.lifted.ToShapedValue [(slick.lifted.Rep [INT],   slick.lifted.Rep [String],slick.lifted.Rep [String],   slick.lifted.Rep [java.time.LocalDateTime])]◾没有足够的参数   方法&lt;&gt; :(隐含证据$ 3:   scala.reflect.ClassTag [tasks.addit.email.MyEmail],隐式形状:   slick.lifted.Shape [_&lt ;: slick.lifted.FlatShapeLevel,   (slick.lifted.Rep [Int],slick.lifted.Rep [String],   slick.lifted.Rep [String],slick.lifted.Rep [java.time.LocalDateTime]),   (Int,String,String,java.time.LocalDateTime,   选项[java.time.LocalDateTime]),   _])slick.lifted.MappedProjection [tasks.addit.email.MyEmail,(Int,String,String,java.time.LocalDateTime,   选项[java.time.LocalDateTime])]。未指定的值参数形状。   ◾实现slick.lifted.AbstractTable。$次◾没有匹配的形状   找到。 Slick不知道如何映射给定的类型。可能   原因:表[T]中的T与您的*投影不匹配。或者你使用   查询中不支持的类型(例如scala列表)。所需级别:   slick.lifted.FlatShapeLevel源类型:(slick.lifted.Rep [Int],   slick.lifted.Rep [String],slick.lifted.Rep [String],   slick.lifted.Rep [java.time.LocalDateTime])解压缩类型:(Int,   String,String,java.time.LocalDateTime,   选项[java.time.LocalDateTime])打包类型:任何

0 个答案:

没有答案