Scala-slick-获取包装选项的TyedType[T] [英] Scala - Slick - Getting a TypedType for a wrapped Option[T]
本文介绍了Scala-slick-获取包装选项的TyedType[T]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常会这样创建自定义ID:
case class CustomID(value: Int) extends MappedTo[Int]
并使用Option[CustomID]等类型表示可为空的自定义ID。但是,我希望能够将选项[_]移到Case类中,如下所示:
case class OptCustomID(optValue: Option[Int])
更具体地说,我正在寻找与数据库DDL相关的TyedType[Option[Int]],它的行为类似于内置的TyedType[Option[Int]]。
有什么想法吗?
推荐答案
其实不需要TypedType[OptCustomId]
。页http://slick.lightbend.com/doc/3.3.0/userdefined.html
OptCustomID
类型的字段的表的正确方法
import slick.jdbc.PostgresProfile.api._
case class OptCustomID(optValue: Option[Int])
case class LiftedOptCustomID(optValue: Rep[Option[Int]])
implicit object OptCustomIDShape extends CaseClassShape(LiftedOptCustomID, OptCustomID)
case class Thing(id: OptCustomID, data: String)
case class LiftedThing(id: LiftedOptCustomID, data: Rep[String])
implicit object ThingShape extends CaseClassShape(LiftedThing.tupled, Thing.tupled)
class ThingTable(tag: Tag) extends Table[Thing](tag, "things") {
def id = column[Option[Int]]("id", O.PrimaryKey)
def data = column[String]("data")
def * = LiftedThing(LiftedOptCustomID(id), data)
}
val things = TableQuery[ThingTable]
val result: DBIO[Option[Thing]] = things.filter(x => x.id === 1).result.headOption
这篇关于Scala-slick-获取包装选项的TyedType[T]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文