【问题标题】:What's DBIO and how can I get the underlying object?什么是 DBIO,如何获取底层对象?
【发布时间】:2015-09-24 21:57:43
【问题描述】:

在使用 Slick 3.0.3 并试图解决这个问题 how-to-use-slick-code-generator-to-include-database-views-as-well 时,我得到了这样的结果:

import slick.dbio.DBIO
import slick.model.Model
import slick.driver.PostgresDriver
import slick.jdbc.meta.MTable
import slick.codegen.SourceCodeGenerator

val model : DBIO[Model] = PostgresDriver.createModel(Some(MTable.getTables(None, None, None, Some(Seq("table1","table2","view1")))))
// SourceCodeGenerator requires a Model and not a DBIO[Model]
val codegen = new SourceCodeGenerator(model)

它看起来几乎就在那里,但是PostgresDriver.createModel 返回一个DBIO[Model] 而不是Model,我不知道如何提取模型或就此而言使用此类模型参考运行生成器。这个 DBIO 到底是什么?

【问题讨论】:

    标签: scala slick


    【解决方案1】:

    DBIO 是DBIOAction 的一种类型。 DBIOAction 是可以对数据库执行的操作。但它还没有在数据库上执行。您需要创建与数据库的连接以获取数据库对象,并将DBIO[Model] 对象传递给它,该对象应返回Future[Model]。

    编辑:

    有关创建数据库对象的信息,请参阅:http://slick.typesafe.com/doc/3.0.2/gettingstarted.html

    您需要在 application.conf 中有正确的配置。然后,您可以使用如下代码:

    val db = Database.forConfig("h2mem1")
        val dbio = PostgresDriver.createModel(Some(MTable.getTables(None, None, None, Some(Seq("table1","table2","view1")))))
        val modelF = db.run(dbio) //Produces Future[Model]
        val genF = modelF.map(model => new SourceCodeGenerator(model)) //Produces Future[SourceCodeGenerator]
    

    【讨论】:

    • 谢谢!您能否通过显示 OP 代码的伪解来完成答案?
    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多