如何在Activator控制台中获取隐式光滑数据库?

时间:2015-01-26 09:05:57

标签: scala playframework playframework-2.0 sbt slick

我尝试在REPL中调试一些函数:

activator console -Dconfig.file=conf/test.conf
scala> new play.core.StaticApplication(new java.io.File("."))
scala> import models._

上面的行很有效,但是当我调用依赖于隐式数据库会话的方法时,它会抱怨:

scala> Employee.listProjectsByEmployee(5)
<console>:11: error: could not find implicit value for parameter db: play.api.db.slick.Database
              Employee.listProjectsByEmployee(5)

我尝试导入这样的数据库值,但它没有工作:

scala>   implicit val myDb = play.api.db.slick.DB
<console>:10: error: You do not have an implicit Application in scope. If you want to bring the current running Application into context, just add import play.api.Play.current
         implicit val myDb = play.api.db.slick.DB

我也尝试导入Play.current,但它也没有用。

scala> import play.api.Play.current
import play.api.Play.current

scala>   implicit val myDb = play.api.db.slick.DB
<console>:12: error: You do not have an implicit Application in scope. If you want to bring the current running Application into context, just add import play.api.Play.current
         implicit val myDb = play.api.db.slick.DB

这也不起作用..

implicit val myDb2 = play.api.db.slick.Database
myDb2: play.api.db.slick.Database.type = play.api.db.slick.Database$@7e0b6883

有没有人有关于如何导入隐式play.api.db.slick.Database的想法?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用测试控制台中的FakeApplication

$ activator test:console

scala> import play.api._

scala> import play.api.test.FakeApplication

scala> Play.start(FakeApplication())

我的数据库是postgresql:

scala> import play.api.db.slick.DatabaseConfigProvider

scala> import slick.driver.JdbcProfile

scala> import slick.driver.PostgresDriver.api._

scala> implicit val db = DatabaseConfigProvider.get[JdbcProfile](Play.current).db

--> db:  slick.driver.JdbcProfile#Backend#Database =
         slick.jdbc.JdbcBackend$DatabaseDef@5107d916