0

尝试将信息插入到如下所示的数据库中:

(UUID, EnumType)

具有以下逻辑:

var t = TestTable.query.map(t=> (t.id, t.enumType)) ++= toAdd.map(idTest, enumTest)))

但是编译器会抛出一个错误,因为TestTable.query.map(t=> (t.id, t.enumType))它将它解释为 type 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
  )
4

1 回答 1

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]
}

要将 enum 映射到 SQL 数据类型 slick 需要隐式MappedColumnType

implicit val colorTypeColumnMapper: JdbcType[Color.Value] = MappedColumnType.base[Color.Value, String](
  e => e.toString,
  s => Color.withName(s)
)

现在您可以通过这种方式将值插入 DB:

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)
)
于 2018-12-10T11:23:32.307 回答