gpt4 book ai didi

Scala - Slick - 为包装的选项获取 TypedType [T]

转载 作者:行者123 更新时间:2023-12-05 08:07:29 24 4
gpt4 key购买 nike

通常会像这样创建自定义 ID:

case class CustomID(value: Int) extends MappedTo[Int]

并用 Option[CustomID] 等类型表示可为 null 的自定义 ID。但是,我希望能够将 Option[_] 移动到案例类中,如下所示:

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 类型字段的表的正确方法

  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 - 为包装的选项获取 TypedType [T],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55100254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com