枚舉值我有後續枚舉:如何堅持光滑
object LoginStatus extends Enumeration() with BitmaskedEnumeration {
type LoginStatus = Value
val Active = Value("A")
val Inactive = Value("I")
}
我需要堅持枚舉的「A」的值,但生成的SQL時,結果爲0。 這是表映射:
object LoginTable extends Table[Login]("login") {
def idLogin = column[Int]("idlogin", O.PrimaryKey, O.AutoInc)
def cdLogin = column[String]("cdlogin", O.NotNull)
def cdPass = column[String]("cdPass", O.NotNull)
def stLogin = column[LoginStatus]("stlogin", O.NotNull, O.DBType("character(1)"))
}
如何持久枚舉值?
我實現
implicit val charMapper = MappedTypeMapper.base[Char, String](
b => b.toString(),
i => i.charAt(0))
implicit def enum2StringMapper(enum: Enumeration) = MappedTypeMapper.base[enum.Value, Char](
b => b.toString.charAt(0),
i => enum.withName(i.toString))
implicit val LoginStatusMapper = enum2StringMapper(LoginStatus)
,反而會導致:
implicit val LoginStatusTypeMapper = MappedTypeMapper.base[LoginStatus.Value, Int]( // conversion from LoginStatus to int { status => status.id }, // conversion back from int to enum { id => LoginStatus(id) } )
,那麼你需要引用您的列:
[error] c.Login - Invalid value for type int : A
這是有點相關http://stackoverflow.com/questions/19030875/how-can-i-create-a-custom-column-type-with-typesafe-slick-in-scala/19040124#19040124 – cvogt
這個也在Slick郵件列表上討論https://groups.google.com/d/msg/scalaquery/Cd5iG-tJchM/fEIhq8IPVJQJ – cvogt
也與http://stackoverflow.com/questions/18752929/how-to-use相關-sens-in-scala-slick – cvogt