Search code examples
scalaslick

List of tables from slick generated schema


I want to get list of tables from slick generated schema. I saw the there is generated: lazy val schema: profile.SchemaDescription = Table1.schema ++ Table2.schema ++ ...

But I see no way of extracting tables out of it. I could go with schema.cereateStatements and filter on create table, to extract the table names but it's not helpful.


Solution

  • You can read tables with MTable class

    import scala.concurrent.ExecutionContext.Implicits.global
    import slick.jdbc.meta.MTable 
    
    //Tables from public schema
    
        db.run(MTable.getTables(Some(""),Some("public"),Some(""),Some(Seq("TABLE")))).
           onComplete {
               case Success(tables) => println(tables.map(_.name).mkString(" - "))
               case Failure(f) => println(f)
           }
    

    The result is:

    MQName(public.table1) - MQName(public.table2) 
    

    If you want Indexes too, add "INDEX" to Seq

    Some(Seq("TABLE","INDEX"))