Slick不知道如何映射给定的类型

时间:2016-12-16 19:54:33

标签: scala slick

以下是我的代码

package com.codemobile.box.modules.groups.repo.sql
import com.codemobile.box.core.services.DatabaseService
import com.codemobile.box.modules.groups.model.GroupEntity
trait GroupEntityTable {
  protected val databaseService: DatabaseService
  import databaseService.driver.api._
  class Groups(tag: Tag) extends Table[GroupEntity](tag, "groups"){
    val id = column[Int]("grp_id",O.PrimaryKey,O.AutoInc)
    val name = column[String]("grp_name")
    val permissions = column[String]("grp_permissions")
    def * = (id, name, permissions) <> (GroupEntity.tupled, GroupEntity.unapply)
  }

  protected val groupsTableQuery = TableQuery[Groups]

}


package com.codemobile.box.modules.groups.model
case class GroupEntity(id: Option[Int] = None, name: String, permissions : Option[String] = None)

然而,当我运行激活器运行时,我得到以下异常

[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[String], slick.lifted.Rep[String], slick.lifted.Rep[Option[Int]])
[error]    Unpacked type: (Option[Int], String, Option[String])
[error]      Packed type: Any
[error]     def * = (name, permissions, id.?) <> (GroupEntity.tupled, GroupEntity.unapply)
[error]                                       ^
[error] one error found

1 个答案:

答案 0 :(得分:4)

只需在映射中添加Option类型:

  class Groups(tag: Tag) extends Table[GroupEntity](tag, "groups"){
    val id = column[Option[Int]]("grp_id",O.PrimaryKey,O.AutoInc)
    val name = column[String]("grp_name")
    val permissions = column[Option[String]]("grp_permissions")
    def * = (id, name, permissions) <> (GroupEntity.tupled, GroupEntity.unapply)
  }

(请注意我对columnid的{​​{1}}进行了更改

或者你也可以这样做:

permissions

(注意我如何更改 class Groups(tag: Tag) extends Table[GroupEntity](tag, "groups"){ val id = column[Int]("grp_id",O.PrimaryKey,O.AutoInc) val name = column[String]("grp_name") val permissions = column[String]("grp_permissions") def * = (id.?, name, permissions.?) <> (GroupEntity.tupled, GroupEntity.unapply) } 投影并在*?之后添加id