I'm using Play framework btw. Slick does not help you "create" tables. Users have to create it by themselves, using tableQuery.ddl.create
. When it was still Slick 1.0, I read a post about creating a Global.scala
and inside it looks like this:
import play.api._
import scala.slick.jdbc.meta.MTable
import play.api.Play.current
import play.api.db.slick._
import models.current.dao._
import models.current.dao.profile.simple._
object Global extends GlobalSettings {
override def onStart(app: Application) {
DB.withSession {implicit rs =>
databaseCreate(Articles, Labels, Article_Labels, Users, Messages)
def databaseCreate(tables: TableQuery[_]*) = {
for (table <- tables)
if (MTable.getTables(table.toString).list.isEmpty) table.ddl.create
}
}
}
}
However, since it is Slick 2.0 and I'm using the Cake pattern, this doesn't work anymore. I used to be able to pull Table
class out and there is an attribute called name
which I can call, but now with the new and improved Cake pattern, all I'm left with is TableQuery
, which literally has nothing. (Cake pattern example is here: https://github.com/slick/slick-examples/blob/master/src/main/scala/com/typesafe/slick/examples/lifted/MultiDBCakeExample.scala)
My DAO class looks like this:
class DAO(override val profile: JdbcProfile) extends ArticleComponent with Article_LabelComponent with LabelComponent with MessageComponent with UserComponent with Profile {
val Articles = TableQuery[Articles]
val Article_Labels = TableQuery[Article_Labels]
val Labels = TableQuery[Labels]
val Messages = TableQuery[Messages]
val Users = TableQuery[Users]
}
object current {
val dao = new DAO(DB(play.api.Play.current).driver)
}
How should I modify my Global.scala
to create a table if a table is not present in the Database?