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

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

最佳答案

该问题与Blob支持无关。如错误消息所示,这是最常见的原因,“表[T]中的T与您的*投影不匹配”。应该是def * = (id, last_updated, data) <> (Test.tupled, Test.unapply _)
请注意,这对于bytea仍然不起作用。Slick的PostgreSQL驱动程序将Blob映射到PotgreSQL的“lo”扩展名中的blob类型。您需要为Array[Byte]使用bytea

关于postgresql - 如何在光滑的2.0.1中表示postgres`bytea`类型?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22914726/

10-10 14:54