Pregunta

Tengo una tabla de postgres.

CREATE TABLE "Contest"
(
  id serial NOT NULL,
  name varchar(100) NOT NULL,
  type char(1) NOT NULL,
  status char(1) NOT NULL,
  ...
)

Estoy intentando obtener valores de campo. type y status Volver a mi aplicación Play 2.x (Anorm):

 val parseContest = {
  get[Pk[Int]]("id") ~
  get[String]("name") ~
  get[Char]("type") ~
  get[Char]("status") map {
  case id~name~c_type~status =>
    Contest(id, name, c_type, status)
  }
}

y obtiene error:

could not find implicit value for parameter extractor: anorm.Column[Char]

Parece que 'Char' no es compatible con una norma.¿Qué debo cambiar en mi código?¿Es una buena práctica utilizar get[String]("status") y luego status.head como solución?

¿Fue útil?

Solución

Creo que podrías escribir un convertidor implícito para tu char columnas.Se vería así:

implicit def columnToChar: Column[Char] = {
    Column[Char](transformer = {
      (value, meta) =>
        val MetaDataItem(qualified, nullable, clazz) = meta
        value match {
          case ch: String => Right(ch.head)
          case _ => Left(TypeDoesNotMatch("Cannot convert " + value + " to Char for column " + qualified))
        }
    })
  }

Entonces asegúrese de que este convertidor esté dentro del alcance.

No estoy seguro de que el valor sea un String pero puedes comprobarlo y hacer las correcciones correspondientes.

Otros consejos

Se fusiona recientemente PR en Anorm sobre eso: https://github.com/playframework/PlayFramework / Pull / 2189

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top