tminglei / slick-pg

Slick extensions for PostgreSQL
BSD 2-Clause "Simplified" License
839 stars 180 forks source link

Usage question: how to use slick-pg with an existing Profile? #559

Open nemoo opened 2 years ago

nemoo commented 2 years ago

I want to use slick-pg with the great https://github.com/gitbucket/blocking-slick

It provides its own api and is used like this: import com.github.takezoe.slick.blocking.BlockingPostgresDriver.blockingApi._

I am strugling to combine the slick-pg with blocking-slick. My first naive approach looks like this:

import com.github.takezoe.slick.blocking.BlockingJdbcProfile
import com.github.tminglei.slickpg._

//trait MyPostgresDriver extends ExPostgresProfile with PgDate2Support {
trait MyPostgresDriver extends BlockingJdbcProfile with PgDate2Support {

  override val api = new API with DateTimeImplicits {}
}

object MyPostgresDriver extends MyPostgresDriver

This fails with Illegal inheritance, self-type MyPostgresDriver does not conform to PgDate2Support with PostgresProfile. I created feature branch for this experiment: https://github.com/nemoo/play-slick-synchronous-example/blob/slick-pg/app/util/MyPostgresDriver.scala

Is there a way to use slick-pg with external Profiles, like the one mentioned above?

nemoo commented 2 years ago

ok, got it to compile by extending both.

import com.github.takezoe.slick.blocking.BlockingJdbcProfile
import com.github.tminglei.slickpg._

trait MyPostgresDriver extends ExPostgresProfile with BlockingJdbcProfile with PgDate2Support {

  override val api = new API with DateTimeImplicits {}
}

object MyPostgresDriver extends MyPostgresDriver

and then using it like this: import util.MyPostgresDriver.blockingApi._

nemoo commented 2 years ago

My simple test case for the use of OffsetDateTime ( https://github.com/nemoo/play-slick-synchronous-example/blob/slick-pg/test/models/ModelScalaTestSpec.scala#L55 )

  "An timestamp " should {
    "be persisted correctly" in {
      db.withSession { implicit session =>
        val projectId = projectRepo.create("A")

        val timestamp = OffsetDateTime.now()
        val taskId = taskRepo.insert(Task(0, "blue", TaskStatus.ready, projectId, timestamp))
        taskRepo.findById(taskId).lastModification mustBe timestamp
      }
    }
  }

is still failing with:

- should be persisted correctly *** FAILED ***
[info]   org.postgresql.util.PSQLException: ERROR: column "last_modification" is of type timestamp with time zone but expression is of type character varying
[info]   Hint: You will need to rewrite or cast the expression.
[info]   Position: 87

Where could I look for the error?