前言
在使用Slick的时候,遇到了各种各样貌似不可控的问题。比如使用同样的语法查一张表可以,但是查另外一张表就报奇奇怪怪的错误。像今天的slick.SlickException: Read NULL value for ResultSet column <computed>。
用Scala的国内目前很少,用Slick就更少了,跌跌撞撞总算找到了答案,现在记录下来。
微信公众号:大数据报文
正文
我的代码:
//查询id为2的数据
val res = Await.result(userDAL.findById(2),Duration.Inf)
override def findById(id: Int): Future[Option[User]] = db.run(userTable.filter(_.id === id).result.headOption)
首先需要看一下自己为表定义的Schema
//这里并没有规定某一列可以是空的
class UserTable(tag: Tag) extends Table[User](tag, "user") {
override def * : ProvenShape[User] = (id, nickName, phone, gender,password) <> (User.tupled,User.unapply)
val id = column[Int]("id")
val nickName = column[String]("nickname")
val phone = column[String]("phone")
val gender = column[Short]("gender")
val password = column[String]("password")
}
再看一下表中的数据,发现id为2的行是空的
是了问题就出在这里。schema中没有为空值规定默认值,而又出现了不允许的空值,因此报错