为什么 MySQLDriver 没有可空列选项?
Why doesn't MySQLDriver have a Nullable Column Option?
我的 table 定义中有这行代码
def firstName = column[Option[String]]("first_name", O.Nullable)
但是我在编译时遇到这个错误
[error] /Users/roy/adivinate/survey2/app/model/Respondent.scala:36:
value Nullable is not a member of slick.driver.MySQLDriver.ColumnOptions
我很困惑,因为这正是我在其他代码中看到的语法,尽管我不确定他们是否使用 MySQLDriver。我应该为空列做什么?
O.Nullable
已弃用。声明字段 Option[T]
即可。
在模型 class 中将您的字段声明为 Option[T]
而不是 T
以使相应的列可空
让我们用一个例子来理解这一点
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
中不需要
我的 table 定义中有这行代码
def firstName = column[Option[String]]("first_name", O.Nullable)
但是我在编译时遇到这个错误
[error] /Users/roy/adivinate/survey2/app/model/Respondent.scala:36:
value Nullable is not a member of slick.driver.MySQLDriver.ColumnOptions
我很困惑,因为这正是我在其他代码中看到的语法,尽管我不确定他们是否使用 MySQLDriver。我应该为空列做什么?
O.Nullable
已弃用。声明字段 Option[T]
即可。
在模型 class 中将您的字段声明为 Option[T]
而不是 T
以使相应的列可空
让我们用一个例子来理解这一点
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
中不需要