在Slick中使用Option而不是NOT NULL

时间:2015-05-13 09:42:53

标签: scala h2 slick

我有一个班级:

class Sitestats(tag: Tag) extends Table[(Int,String,String,Int,String)](tag, "SITESTATS"){
  def id = column[Int]("STAT_ID", O.PrimaryKey, O.AutoInc)
  def url = column[String]("STAT_URL")
  def httpstatus = column[String]("STAT_HTTPSTATUS")
  def contentlength = column[Int]("STAT_CONTENTLENGTH")
  def description = column[String]("STAT_DESC")

  def * : ProvenShape[(Int,String,String,Int,String)] = (id,url,httpstatus,contentlength,description)
}

允许我这样做(使用光滑的)

 sitestats.ddl.create
 sitestats += ((3534,"http://google.com","TEST",22,"TEST"))
 sitestats += ((234,"http://google7.com","404",22,"TEST"))
 println(sitestats.list)

导致预期(剪辑):

[info] List((1,http://google.com,TEST,22,TEST),(2,http://google7.com,404,22,TEST))

我希望httpstatus能够为null,所以我将行改为

def httpstatus = column[Option[String]]("STAT_HTTPSTATUS")

导致

[error]  found   : (scala.slick.lifted.Column[Int], scala.slick.lifted.Column[String], scala.slick.lifted.Column[Option[String]], scala.slick.lifted.Column[Int], scala.slick.lifted.Column[String])
[error]  required: scala.slick.lifted.ProvenShape[(Int, String, String, Int, String)]

我错过了什么?

1 个答案:

答案 0 :(得分:3)

尝试像这样更新:

def * : ProvenShape[(Int,String,Option[String],Int,String)] = (id,url,httpstatus,contentlength,description)

您忘记更新表格定义。 :)

更新

sitestats += (Some(234,"http://google7.com","404",22,"TEST"))