尝试将信息插入数据库,如下所示:
(UUID, EnumType)
具有以下逻辑:
var t = TestTable.query.map(t=> (t.id, t.enumType)) ++= toAdd.map(idTest, enumTest)))
但是编译器针对TestTable.query.map(t=> (t.id, t.enumType))
抛出错误,将其解释为类型Iteratable[Nothing]
,我是否丢失了某些内容?
测试表如下:
object TestTable {
val query = TableQuery[TestTable]
}
class TestTable(tag: slick.lifted.Tag) extends Table[TestTable](tag, "test_table") {
val id = column[UUID]("id")
val enumType = column[EnumType]("enumType")
override val * = (id, testType) <> (
(TestTable.apply _).tupled,
TestTable.unapply
)
答案 0 :(得分:0)
假设您具有以下数据结构:
object Color extends Enumeration {
val Blue = Value("Blue")
val Red = Value("Red")
val Green = Value("Green")
}
case class MyType(id: UUID, color: Color.Value)
如下定义滑动模式:
class TestTable(tag: slick.lifted.Tag) extends Table[MyType](tag, "test_table") {
val id = column[UUID]("id")
val color = column[Color.Value]("color")
override val * = (id, color) <> ((MyType.apply _).tupled, MyType.unapply)
}
object TestTable {
lazy val query = TableQuery[TestTable]
}
要将枚举映射到SQL数据类型,需要隐式MappedColumnType:
implicit val colorTypeColumnMapper: JdbcType[Color.Value] = MappedColumnType.base[Color.Value, String](
e => e.toString,
s => Color.withName(s)
)
现在您可以通过以下方式将值插入数据库:
val singleInsertAction = TestTable.query += MyType(UUID.randomUUID(), Color.Blue)
val batchInsertAction = TestTable.query ++= Seq(
MyType(UUID.randomUUID(), Color.Blue),
MyType(UUID.randomUUID(), Color.Red),
MyType(UUID.randomUUID(), Color.Green)
)