简体   繁体   中英

Slick: could not find implicit value for parameter e: slick.jdbc.SetParameter[Option[java.util.UUID]]

I'm workihng with Play. 2,5. Slick 3 and PostgreSQL 9.6.

I'm trying to use a simple plain SQL query with an optional UUID but I get this error:

Slick: could not find implicit value for parameter e: slick.jdbc.SetParameter[Option[java.util.UUID]]

My case class looks like

final case class GaClientId(ip: String, userId: Option[UUID], clientId: String)

And my query like this:

db.run(       
  sql"""
    INSERT INTO ga_client_id(ip, user_id, clientId) VALUES (
    ${gaClientId.ip}, ${gaClientId.userId}, $gaClientId.userId.orNull)
    ON CONFLICT DO NOTHING;""")

I tried to add this:

implicit val getUUIDContent: GetResult[GaClientId] =
  GetResult(r => GaClientId(r.<<, r.nextStringOption().map(UUID.fromString), r.<<))

but without result.

How can I do this?

As the error implies you should specify SetParameter not GetParameter . It should be as follows:

  implicit val uuidSetter = SetParameter[Option[UUID]] {
    case (Some(uuid), params) => params.setString(uuid.toString)
    case (None, params) => params.setNull(Types.VARCHAR)
  }

And then you should probably add asUpdate to your update SQL :

  db.run((sql"""
      INSERT INTO ga_client_id(ip, user_id, clientId) VALUES (
      ${gaClientId.ip}, ${gaClientId.userId}, ${gaClientId.clientId})
      ON CONFLICT DO NOTHING;
  """).asUpdate)

Based on the answer of @Paul Doelga, you can additionally try

  db.run((sql"""
      INSERT INTO ga_client_id(ip, user_id, clientId) VALUES (
      ${gaClientId.ip}, ${gaClientId.userId}::uuid, ${gaClientId.clientId})
      ON CONFLICT DO NOTHING;
  """).asUpdate)

I originally got the answer from

https://github.com/skinny-framework/skinny-framework/issues/301

I guess the main reason of the failure is that Postgres need a parameter in type UUID , but JDBC doesn't support it, so you have to convert it to String , and convert back to UUID in SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM