Scala Slick Class Table 定义,声明日期时间列时出错
Scala Slick Class Table Definition, Error Declaring DateTime columns
下面是数据库Class
进行 table 定义时,日期时间列出现编译错误。
Created_on 应该在更新当前日期
updated_on 应该只是模拟通过的日期。
包tables
import play.data.format.Formats.DateTime
import slick.driver.PostgresDriver.api._
object Main {
case class Account(
id: Long = 0L,
owner: Long,
name: String,
created_on: DateTime,
updated_on: DateTime,
author_id: Long,
updated_by: Long,
external_id: String
)
class AccountTable(标签:标签)扩展 TableAccount {
def id = column[Long]("id")
def owner= column[Long]("id")
def name= column[String]("name")
def created_on= column[DateTime]("created_on")
def updated_on= column[DateTime]("updated_on")
def author_id= column[Long]("author_id")
def updated_by= column[Long]("updated_by")
def external_id= column[String]("external_id")
def * = (owner, name, created_on,
updated_on,author_id,updated_by,external_id) <> (Account.tupled,
Account.unapply)
}
}
我不确定您使用的是什么 DateTime
class。也许是乔达时代 DateTime
class?
为此,您需要提供从 DateTime
到 Slick 知道的类型(以及对数据库中的列有意义的类型)的映射。例如,您可以将 DateTime
映射到 Timestamp
:
import java.sql.Timestamp
import org.joda.time.DateTime
import org.joda.time.DateTimeZone.UTC
// And in your `Main`...
implicit val jodaDateTimeType =
MappedColumnType.base[DateTime, Timestamp](
dt => new Timestamp(dt.getMillis),
ts => new DateTime(ts.getTime, UTC)
)
...例如。这是在教 Slick 如何从 DateTime
转换为它知道的类型 (Timestamp
)。
Chapter 5 of Essential Slick 中有一个关于此的教程,其中有更多详细信息。
从 Slick 3.3 开始,内置了对许多标准 Java 时间格式的支持,described in the release notes。
此外,自 Slick 3 起,您可以在大多数情况下使用 .mapTo
映射 classes。比 <>
:
好读多了
def * = (
owner, name, created_on, updated_on, author_id, updated_by, external_id
).mapTo[Account]
下面是数据库Class 进行 table 定义时,日期时间列出现编译错误。
Created_on 应该在更新当前日期
updated_on 应该只是模拟通过的日期。
包tables
import play.data.format.Formats.DateTime import slick.driver.PostgresDriver.api._ object Main { case class Account( id: Long = 0L, owner: Long, name: String, created_on: DateTime, updated_on: DateTime, author_id: Long, updated_by: Long, external_id: String )
class AccountTable(标签:标签)扩展 TableAccount {
def id = column[Long]("id") def owner= column[Long]("id") def name= column[String]("name") def created_on= column[DateTime]("created_on") def updated_on= column[DateTime]("updated_on") def author_id= column[Long]("author_id") def updated_by= column[Long]("updated_by") def external_id= column[String]("external_id") def * = (owner, name, created_on, updated_on,author_id,updated_by,external_id) <> (Account.tupled, Account.unapply)
}
}
我不确定您使用的是什么 DateTime
class。也许是乔达时代 DateTime
class?
为此,您需要提供从 DateTime
到 Slick 知道的类型(以及对数据库中的列有意义的类型)的映射。例如,您可以将 DateTime
映射到 Timestamp
:
import java.sql.Timestamp
import org.joda.time.DateTime
import org.joda.time.DateTimeZone.UTC
// And in your `Main`...
implicit val jodaDateTimeType =
MappedColumnType.base[DateTime, Timestamp](
dt => new Timestamp(dt.getMillis),
ts => new DateTime(ts.getTime, UTC)
)
...例如。这是在教 Slick 如何从 DateTime
转换为它知道的类型 (Timestamp
)。
Chapter 5 of Essential Slick 中有一个关于此的教程,其中有更多详细信息。
从 Slick 3.3 开始,内置了对许多标准 Java 时间格式的支持,described in the release notes。
此外,自 Slick 3 起,您可以在大多数情况下使用 .mapTo
映射 classes。比 <>
:
def * = (
owner, name, created_on, updated_on, author_id, updated_by, external_id
).mapTo[Account]