Search code examples
sqlscalaslick

How to create database schema using slick?


I have tried

val schemas = addresses.schema

val setup = schemas.create

val db = Database.forConfig("h2disk")

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

but, apparently, it is not working. Here are some logs

[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)

Solution

  • We can try

    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)
    

    Notes: the schema name for some dbms is case sensitive, e.g. H2 will automatically convert schema to APPS