Dynamic table name in Slick 2.0.0.0

454 views
Skip to first unread message

Madhur Kumar

unread,
Jan 7, 2014, 5:41:28 AM1/7/14
to scala...@googlegroups.com

I would like to pass table name dynamically as given below to TableQuery object, I was able to do in Slick 1.0 as given in this question

Dynamic table name override in Slick table

But not sure 2.0 allow some way to achieve the same.

    class MyTable(tag: Tag, tableName: String) extends Table[(String)](tag, tableName){
              def id = column[String]("ROWID", O.PrimaryKey)
              def tNum = column[Int]("T_NUM")
              def tName = column[Int]("T_NAME")
              def * = id
     }

     val tQ = TableQuery[MyTable] filter ...

Richard Pianka

unread,
Jan 7, 2014, 7:52:29 AM1/7/14
to scala...@googlegroups.com
I'm unsure if there's a more idiomatic way to do it, but TableQuery requires a constructor function (which is normally produced by a macro to just pass in tag), so you could supply your own like so:

    val tQ = TableQuery[MyTable](tag => new MyTable(tag, "SomeTableName")) filter ...

Madhur Kumar

unread,
Jan 7, 2014, 8:51:06 AM1/7/14
to scala...@googlegroups.com
Thanks this worked for me with minor change

val tQ = TableQuery[MyTable]((tag:Tag) => new MyTable(tag, "SomeTableName")) filter ...
Reply all
Reply to author
Forward
0 new messages