这个表定义一直有效,直到我意识到拥有可空列意味着我需要使用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
答案 0 :(得分:3)
将模型类中的字段声明为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中的含义是直接转换为sql中的Nullable。
旧版本的Slick
您必须通过在列声明中显式传递O.NotNull来告知特定列不为空,但在新版本的光滑中不需要它
答案 1 :(得分:3)
不要在列的定义中使用选项。如果你有Option [String]列,那么仍然应该是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)
}
答案 2 :(得分:2)
这基本上是一件简单的事情 - 您的案例类需要将这些字段设为可选。例如。而不是(在你的案例中):ng-if
你应该LoremPixel
。