首页 > 解决方案 > 如何将 Duration 类型映射到 Interval?

问题描述

我正在创建一个包含一个Interval字段的 Slick Postresql 表,我想将其表示为Duration

case class Object(id: String, aproxDuration: Duration)
class Objects(tag: Tag) extends Table[Object](tag, "OBJECTS"){
  def id = column[String]("id", O.PrimaryKey)
  def expectedDuration = column[Duration]("expected_duration")
  def * = (id, expectedDuration) <> (Object.tupled, Object.unapply)
}

为了支持这一点,我安装了slick-pg扩展并创建了一个扩展的配置文件,ExPostgresProfile, PgDate2Support, PgRangeSupport但我不确定为什么它没有找到隐含TypedTypeDuration

标签: postgresqlscalaslick

解决方案


import java.time.Duration

import com.github.tminglei.slickpg.{ExPostgresProfile, PgDate2Support}
import slick.lifted.ProvenShape

trait MyPostgresProfile extends ExPostgresProfile with PgDate2Support {

  override val api = MyAPI

  object MyAPI extends API with DateTimeImplicits
}

object MyPostgresProfile extends MyPostgresProfile

case class Object(id: String, aproxDuration: Duration)

object Tables {

  import MyPostgresProfile.api._

  class Objects(tag: Tag) extends Table[Object](tag, "OBJECTS") {
    def id: Rep[String]                 = column[String]("id", O.PrimaryKey)
    def expectedDuration: Rep[Duration] = column[Duration]("expected_duration")
    def * : ProvenShape[Object]         = (id, expectedDuration) <> (Object.tupled, Object.unapply)
  }

}

推荐阅读