B R*_*son 3 scala codegen playframework slick
这个表定义一直有效,直到我意识到具有可为空的列意味着我需要使用 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)
}
Run Code Online (Sandbox Code Playgroud)
我在编译时收到此错误。
[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
Run Code Online (Sandbox Code Playgroud)
小智 6
Don't use option in definition of the column. If you have Option[String] column should be still String.
def firstName = column[Option[String]]("first_name") //Bad
def firstName = column[String]("first_name") //Good
Run Code Online (Sandbox Code Playgroud)
When you define select (def *) use .? to define Option
override def * =
(id, uuid, version, task, firstName.?, lastName.?, ageGroup.?, incomeLevel.?, employmentStatus.?, maritalStatus.?, housingStatus.?, educationStatus.?, gender.?) <> (Respondent.tupled, Respondent.unapply)
Run Code Online (Sandbox Code Playgroud)
Also if you want to update (in TableQuery):
def updateExistingName(name : String) : DBIO[Int] =
{
map(_.firstName).update(name)
}
def updateOptionName(nameCanBeNone: Option[String]) : DBIO[Int] =
{
map(_.firstName.?).update(nameCanBeNone)
}
Run Code Online (Sandbox Code Playgroud)
这基本上是一件简单的事情 - 您的案例类需要有这些字段可选。例如,而不是(在您的案例类中):firstName: String您应该拥有firstName: Option[String].
在模型类中声明您的字段,而Option[T]不是T使相应的列可为 Null
让我们通过一个例子来理解这一点
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)
}
Run Code Online (Sandbox Code Playgroud)
如果你想让某些东西可以为空,只需将其声明为Option字段,这样 slick 就会理解并生成该特定字段为可空的 sql。
上面的设计与 Scala Option 设计是无缝且合理的。意思是Scala中的option直接转换为sql中的Nullable。
在旧版本的 Slick 中
您必须通过在列声明中显式传递 O.NotNull 来告诉特定列不为空,但在新版本的 slick 中不需要它