for weeks I'm trying to get slick running with evolutions for Postgres (and eventually with codegen for the Tables.scala file) and it is extremely frustrating because the official documentation stops after the essential basic setup and anything that can be found elsewhere on the internet what goes a bit deeper is outdated. It seems that the framework changes a lot after every release and you cannot use older code snippets.
In order to keep things simple, I ended up in cloning the official play-samples repo and step by step change that to work with Postgres instead of the used H2 in-memory db.
Here is a link to the play-scala-slick-example
The only thing I changed is in the build.sbt#L11:
- I replaced the
h2dependency with a postgres dependency:"org.postgresql" % "postgresql" % "42.2.19", - and I added the
jdbcdependency (according to their documentation) - but this causes a binding issue, so I'm not sure that this dependency should be really used
Then I changed the config to connect to local dev DB at the application.conf#L66-L68
slick.dbs.default.profile="slick.jdbc.PostgresProfile$"
slick.dbs.default.db.dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="jdbc:postgresql://localhost:5432/my_local_test_db?currentSchema=play_example&user=postgres&password="
When I run the application with the JDBC connection and try to access it in the browser it reports this exception:
CreationException: Unable to create injector, see the following errors:
1) A binding to play.api.db.DBApi was already configured at play.api.db.DBModule$$anonfun$$lessinit$greater$1.apply(DBModule.scala:39):
Binding(interface play.api.db.DBApi to ProviderConstructionTarget(class play.api.db.DBApiProvider)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4).
at play.api.db.slick.evolutions.EvolutionsModule.bindings(EvolutionsModule.scala:15):
Binding(interface play.api.db.DBApi to ConstructionTarget(class play.api.db.slick.evolutions.internal.DBApiAdapter) in interface javax.inject.Singleton) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$4)
And when I run it without the jdbc dependency (I saw some hints on stackoverflow that this might cause the exception above) then it displays a warning and eventually runs into a timeout.
[warn] c.z.h.HikariConfig - db - using dataSourceClassName and ignoring jdbcUrl.
Does anyone know what is missing here to tell it to use the JDBC URL?
I was going through similar problems for getting the
Playto work withPostgres.The connections to the database should be configured with a pool from
HikariCP. The pasted configuration, as it can be implied from the build file, getsSlickto useHikariCP.Here you go with my base and let me know if it works:
Contents of
build.sbt:The database configuration which should be added to
application.conf, provided thatPostgresis configured to use encrypted connection. I usedletsencrypt.Sample from
UserDAOImpl, for the signature and the imports:Place this
DatabaseExecutionContextfile in the same directory as yourdaofiles.