试图将信息插入DB中,如下所示:
(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
)
假设您有以下数据结构:
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数据类型slick,需要隐式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)
)