How could I know if a database table is exists in ScalaQuery

前端 未结 6 954
日久生厌
日久生厌 2020-12-29 13:15

I\'m trying ScalaQuery, it is really amazing. I could defined the database table using Scala class, and query it easily.

But I would like to know, in the following c

6条回答
  •  一生所求
    2020-12-29 13:51

    See also the related discussion here.I personally prefer hezamu's suggestion and extend it as follows to keep it DRY:

    def createIfNotExists(tables: TableQuery[_ <: Table[_]]*)(implicit session: Session) {
      tables foreach {table => if(MTable.getTables(table.baseTableRow.tableName).list.isEmpty) table.ddl.create}
    }
    

    Then you can just create your tables with the implicit session:

    db withSession {
      implicit session =>
        createIfNotExists(table1, table2, ..., tablen)
    }
    

提交回复
热议问题