14

How do you check if a table exists with slick 3.0?

There was a way in previous versions of slick by using:

MTable.getTables.list()

But this doesn't compile anymore.

The idea behind this question is to dynamically create a table when it doesn't exists, pretty much like this:

if (Tables.contains("USERS") == false)
    Users.createTable()
Cœur
  • 37,241
  • 25
  • 195
  • 267
Benjamin
  • 143
  • 2
  • 7

1 Answers1

18

With Slick 3.0 MTable.getTables is a DBAction which was to be run via a Database instance:

val tables = Await.result(db.run(MTable.getTables), 1.seconds).toList

Of course, you should probable deal with the Future returned by db.run in an asynchronous manner (via map or for-comprehension) rather than blocking on it as I did for the example.

Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
  • is toList method call supported by that query? – stian Jul 14 '16 at 08:45
  • 2
    The return value of `MTable.getTables` is a stream which is an implementation of `Seq` hence you can get a list by calling the function `toList` because every implementation of `Seq` needs to implement a `toList` function –  Jul 15 '16 at 07:23