scalaslickslick-3.0

How to write custom scala slick column mapping


My use case is very simple.

I have wrapper class around string in scala and I want to map it to String type in database using my custom scala mapper code (class is 3rd party and I can't modify it.

All examples I found so far referring to MappedColumnType which does not seems to exist in Slick 3.

Any help and ideally working examples are appreciated.


Solution

  • Looks like you might be missing the driver api import!

    For some reference, have a look at my example here:

    https://github.com/joesan/plant-simulator/blob/master/app/com/inland24/plantsim/services/database/DBSchema.scala

    implicit def powerPlantTypeMapping =
        MappedColumnType.base[PowerPlantType, String](
          powerPlantType => PowerPlantType.toString(powerPlantType),
          powerPlantTypeStr => PowerPlantType.fromString(powerPlantTypeStr)
        )
    

    You need to import:

    import driver.api._