Scala - Slick - 为包装的选项获取 TypedType [T]
Scala - Slick - Getting a TypedType for a wrapped Option[T]
通常这样创建自定义 ID:
case class CustomID(value: Int) extends MappedTo[Int]
并使用 Option[CustomID] 等类型表示可为 null 的自定义 ID。但是,我希望能够将 Option[_] 移动到案例 class 中,如下所示:
case class OptCustomID(optValue: Option[Int])
更具体地说,我正在寻找一个 TypedType[OptCustomId],它的行为类似于内置的 TypedType[Option[Int]],用于数据库 DDL。
有什么想法吗?
其实你不需要TypedType[OptCustomId]
。在页面 http://slick.lightbend.com/doc/3.3.0/userdefined.html
中描述了使用 OptCustomID
类型字段处理 table 的正确方法
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
通常这样创建自定义 ID:
case class CustomID(value: Int) extends MappedTo[Int]
并使用 Option[CustomID] 等类型表示可为 null 的自定义 ID。但是,我希望能够将 Option[_] 移动到案例 class 中,如下所示:
case class OptCustomID(optValue: Option[Int])
更具体地说,我正在寻找一个 TypedType[OptCustomId],它的行为类似于内置的 TypedType[Option[Int]],用于数据库 DDL。
有什么想法吗?
其实你不需要TypedType[OptCustomId]
。在页面 http://slick.lightbend.com/doc/3.3.0/userdefined.html
OptCustomID
类型字段处理 table 的正确方法
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