4

这个表定义一直有效,直到我意识到拥有可为空的列意味着我需要使用 Option[String] 而不仅仅是 String。这是我所做的唯一更改,这就是我的代码现在的样子。

class RespondentTableDef(tag: Tag) extends Table[Respondent](tag, "respondent") {

  def id = column[Long]("id", O.PrimaryKey)
  def uuid = column[String]("uuid")
  def version = column[Long]("version")
  def task = column[Long]("task")
  def firstName = column[Option[String]]("first_name")
  def lastName = column[Option[String]]("last_name")
  def ageGroup = column[Option[String]]("age_group")
  def incomeLevel = column[Option[String]]("income_level")
  def employmentStatus = column[Option[String]]("employment_status")
  def maritalStatus = column[Option[String]]("marital_status")
  def housingStatus = column[Option[String]]("housing_status")
  def educationStatus = column[Option[String]]("education_status")
  def gender = column[Option[String]]("gender")

  override def * =
    (id, uuid, version, task, firstName, lastName, ageGroup, incomeLevel, employmentStatus, maritalStatus, housingStatus, educationStatus, gender) <> (Respondent.tupled, Respondent.unapply)
}

编译时出现此错误。

[error] /Users/roy/adivinate/survey2/app/model/Respondent.scala:45: No matching Shape found.
[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
[error]   Required level: slick.lifted.FlatShapeLevel
[error]      Source type: (slick.lifted.Rep[Long], slick.lifted.Rep[String], slick.lifted.Rep[Long], slick.lifted.Rep[Long], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]], slick.lifted.Rep[Option[String]])
[error]    Unpacked type: (Long, String, Long, Long, String, String, String, String, String, String, String, String, String)
[error]      Packed type: Any
[error]     (id, uuid, version, task, firstName, lastName, ageGroup, incomeLevel, employmentStatus, maritalStatus, housingStatus, educationStatus, gender) <> (Respondent.tupled, Respondent.unapply)
[error]                                                                                                                                                    ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 5 s, completed Dec 21, 2016 8:53:17 PM
4

3 回答 3

5

不要在列的定义中使用选项。如果你有 Option[String] 列应该仍然是字符串。

def firstName = column[Option[String]]("first_name") //Bad
def firstName = column[String]("first_name") //Good

当你定义 select (def *) 使用 .? 定义选项

  override def * =
    (id, uuid, version, task, firstName.?, lastName.?, ageGroup.?, incomeLevel.?, employmentStatus.?, maritalStatus.?, housingStatus.?, educationStatus.?, gender.?) <> (Respondent.tupled, Respondent.unapply)

此外,如果您想更新(在 TableQuery 中):

def updateExistingName(name : String) : DBIO[Int] =
{
    map(_.firstName).update(name)
}

def updateOptionName(nameCanBeNone: Option[String]) : DBIO[Int] =
{
    map(_.firstName.?).update(nameCanBeNone)
}
于 2016-12-22T13:15:06.957 回答
5

在模型类中声明您的字段,Option[T]而不是T使相应的列 Nullable

让我们通过一个例子来理解这一点

case class Foo(name: String, rating: Option[Int])

class Foos(tag: Tag) extends Table[Foo](tag, "foos") {
  def name = column[String]("name") //name is not null
  def rating = column[Option[Int]]("rating") //rating is nullable
  def * = (name, rating) <> (Foo.tupled, Foo.unapply)
}

如果您想使某些内容可以为空,只需将其声明为Option字段,这样它就会理解并生成该特定字段为可空的 sql。

上述设计与 Scala Option 设计是无缝且合理的。意思是Scala中的option直接转换为sql中的Nullable。

在旧版本的 Slick 中

您必须通过在列声明中显式传递 O.NotNull 来告诉特定列不为空,但在新版本的 slick 中不需要它

于 2016-12-22T10:19:29.060 回答
5

这基本上是一件简单的事情——你的案例类需要这些字段是可选的。例如,而不是拥有(在您的案例类中):firstName: String您应该拥有firstName: Option[String].

于 2016-12-22T08:45:54.300 回答