如何使用光滑创建数据库模式?

时间:2017-08-08 16:53:14

标签: sql scala slick

我试过了

val schemas = addresses.schema

val setup = schemas.create

val db = Database.forConfig("h2disk")

Await.result(db.run(setup), Duration.Inf)
但是,显然,它不起作用。这是一些日志

[error] Caused by: org.h2.jdbc.JdbcSQLException: Schema "apps" not found; SQL statement:
[error] create table "apps"."t_address" ("name" VARCHAR,"domain" VARCHAR,"t_address_id" VARCHAR NOT NULL PRIMARY KEY) [90079-196]
[error]         at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
[error]         at org.h2.message.DbException.get(DbException.java:179)
[error]         at org.h2.message.DbException.get(DbException.java:155)
[error]         at org.h2.command.Parser.getSchema(Parser.java:688)
[error]         at org.h2.command.Parser.getSchema(Parser.java:694)

2 个答案:

答案 0 :(得分:1)

我们可以尝试

val schemas = addresses.schema

val setup = DBIO.seq(sqlu"""create schema apps;""", schemas.create)

val db = Database.forConfig("h2disk")

Await.result(db.run(setup), Duration.Inf)

注意:某些dbms的模式名称区分大小写,例如H2会自动将架构转换为APPS

答案 1 :(得分:0)

我必须使用绑定变量来使其按如下方式工作:(用 # 前缀变量)

源代码:https://scala-slick.org/doc/3.3.3/sql.html#splicing-literal-values

 val schemaName = "something"
 val schemas = Cases(schemaName).schema
 val setup = DBIO.seq(
      sqlu"""create schema #${schemaName} AUTHORIZATION postgres""",
      // create table schemas
      schema.createIfNotExists
      //add default data
 ...


      // add rights
...
    )

所有的表都定义为

 class Cases(_tableTag: Tag, schemaName: String) extends profile.api.Table[CasesRow](_tableTag, Some(schemaName), "cases") {
....
}
def Cases(schema: String) = new TableQuery(tag => new Cases(tag,schemaName = schema))