Search code examples
postgresqlscalaslick

How to map Map[String, String] to (String, String) in slick 3.2.0


I am trying to update the hstore field in my Postgres table. It throws an error

Error:(468, 13) 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,
 you use an unsupported type in a Query (e.g. scala List),
 or you forgot to import a driver api into scope.
  Required level: slick.lifted.FlatShapeLevel
     Source type: slick.lifted.Rep[Option[scala.collection.immutable.Map[String,String]]]
   Unpacked type: T
     Packed type: G
        .map(_.additionalInfo).update(Some(info.additionalInfo))

I have added hstore implicit as well

trait MyPostgresDriver extends ExPostgresProfile with PgHStoreSupport {

  override val api = MyAPI

  // Add back `capabilities.insertOrUpdate` to enable native `upsert` support; for postgres 9.5+
  override protected def computeCapabilities: Set[Capability] =
    super.computeCapabilities + JdbcProfile.capabilities.insertOrUpdate


  object MyAPI extends API with HStoreImplicits

}

After browsing I found to create a custom mapping for such scenarios.

implicit val stringMapper = MappedColumnType.base[Map[String, String], (String, String)](
    list => (list.keys.toString(), list.values.toString()),
    string => Map(string._1 -> string._2)
  )

But above custom mapping is not correct. I found this blog But it Implicits is not there in version 3.x

Any help will be appreciated.


Solution

  • As per the error, slick implicit is missing which help you to use Map[String, String] as PgHStore. You need to add (implicit a: JdbcType[Map[String, String]]) in the scope of your code block. which is I guess provided by HStoreImplicits Or PgHStoreSupport.