2

在 slick 的文档中,据说这java.sql.Blob是开箱即用支持的类型之一,我正在尝试使用它来表示bytea列。但是下面的代码给了我一个错误:

import scala.slick.driver.PostgresDriver.simple._
import java.sql.{ Timestamp, Blob }

case class Test(id: Int, last_updated: Timestamp, data: Blob)

class Tests(tag: Tag) extends Table[Test](tag, "tests") {
  def data = column[Blob]("data")
  def last_updated = column[Timestamp]("last_updated")
  def id = column[Int]("id", O.PrimaryKey)
  def * = (id, last_updated, data)
}

错误信息是:

Multiple markers at this line
    - No matching Shape found. Slick does not know how to map the given types. Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported 
     type in a Query (e.g. scala List). Required level: scala.slick.lifted.ShapeLevel.Flat Source type: (scala.slick.lifted.Column[Int], scala.slick.lifted.Column[java.sql.Timestamp], 
     scala.slick.lifted.Column[java.sql.Blob]) Unpacked type: utils.Test Packed type: Any
    - implements scala.slick.lifted.AbstractTable.$times
    - type mismatch; found : (scala.slick.lifted.Column[Int], scala.slick.lifted.Column[java.sql.Timestamp], scala.slick.lifted.Column[java.sql.Blob]) required: 
     scala.slick.lifted.ProvenShape[utils.Test]
    - type mismatch; found : (scala.slick.lifted.Column[Int], scala.slick.lifted.Column[java.sql.Timestamp], scala.slick.lifted.Column[java.sql.Blob]) required: 
     scala.slick.lifted.ProvenShape[utils.Test]

发生了什么事,我怎样才能正确地bytea用 slick 表示?

4

1 回答 1

7

该问题与Blob支持无关。正如错误消息所暗示的最常见原因,“Table[T] 中的 T 与您的 * 投影不匹配”。应该是def * = (id, last_updated, data) <> (Test.tupled, Test.unapply _)

请注意,这仍然不适用于bytea. Slick 的 PostgreSQL 驱动程序映射Blob到来自 PotgreSQL 的“lo”扩展的 blob 类型。您需要Array[Byte]使用bytea.

于 2014-04-07T15:18:16.037 回答