Cab*_*ero 5 postgresql json scala playframework slick
我在我的Scala/Play应用程序中使用Slick 2代码生成器来生成PostgreSQL数据库的表类.有些字段是JSON类型,它们由生成器转换为String.我想知道我是否能以某种方式使用slick-pg插件让发生器识别Postgres JSON类型?
我试图slick.driver.PostgresDriver直接扩展Build.scala:
import slick.driver.PostgresDriver
import com.github.tminglei.slickpg._
trait MyPostgresDriver extends PostgresDriver
with PgArraySupport
with PgDateSupport
with PgRangeSupport
with PgHStoreSupport
with PgPlayJsonSupport
with PgSearchSupport
with PgPostGISSupport {
override val Implicit = new ImplicitsPlus {}
override val simple = new SimpleQLPlus {}
trait ImplicitsPlus extends Implicits
with ArrayImplicits
with DateTimeImplicits
with RangeImplicits
with HStoreImplicits
with JsonImplicits
with SearchImplicits
with PostGISImplicits
trait SimpleQLPlus extends SimpleQL
with ImplicitsPlus
with SearchAssistants
with PostGISAssistants
}
object MyPostgresDriver extends MyPostgresDriver
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用代码生成器例程而不是标准驱动程序:
SourceCodeGenerator.main(
Array(
"scala.slick.driver.PostgresDriver", //how do I use MyPostgresDriver here?
"org.postgresql.Driver",
"jdbc:postgresql://localhost:5432/db?user=root&password=root",
"app",
"db"
)
)
Run Code Online (Sandbox Code Playgroud)
你不能让生成器以这种方式选择类型。它(或者更确切地说,从数据库模式对模型进行逆向工程的 Slick 代码)目前仅检测到全部类型,并简单地假设所有其他类型都是 String 。今后这一点将会得到改善。为了使其对列使用不同的类型,您必须自定义生成器。相应的Slick文档示例实际上展示了如何自定义类型:
http://slick.typesafe.com/doc/2.0.0/code- Generation.html#customization