【问题标题】:Using Option instead of NOT NULL in Slick在 Slick 中使用 Option 而不是 NOT NULL
【发布时间】:2015-07-24 12:30:59
【问题描述】:

我有一堂课:

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)
}

允许我做(使用 slick)

 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 能够为空,所以我将行更改为

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)]

我错过了什么?

【问题讨论】:

    标签: scala h2 slick


    【解决方案1】:

    尝试像这样更新:

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

    您忘记更新表定义。 :)

    更新

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多