postgresqlslickslick-pg

Enum in Plain SQL when using Slick 3.1


I'm using Slick 3.1.0 and Slick-pg 0.10.0. I have an enum as:

object UserProviders extends Enumeration {
  type Provider = Value
  val Google, Facebook = Value
}

Following the test case, it works fine with the column mapper simply adding the following implicit mapper into my customized driver.

implicit val userProviderMapper = createEnumJdbcType("UserProvider", UserProviders, quoteName = true)

However, when using plain SQL, I encountered the following compilation error:

could not find implicit value for parameter e: slick.jdbc.SetParameter[Option[models.UserProviders.Provider]]

I could not find any document about this. How can I write plain SQL with enum in slick? Thanks.


Solution

  • You need to have an implicit of type SetParameter[T] in scope which tells slick how to set parameters from some custom type T that it doesn't already know about. For example:

      implicit val setInstant: SetParameter[Instant] = SetParameter { (instant, pp) => 
        pp.setTimestamp(new Timestamp(instant.toEpochMilli))
      }
    

    The type of pp is PositionedParameters.

    You might also come across the need to tell slick how to extract a query result into some custom type T that it doesn't already know about. For this, you need an implicit GetResult[T] in scope. For example:

      implicit def getInstant(implicit get: GetResult[Long]): GetResult[Instant] = 
        get andThen (Instant.ofEpochMilli(_))