Slick implicit parameter 'tables' with generated tables

250 views Asked by At

The simple version

What's the preferred way to import and use generated Slick tables?

The detailed version and what I've tried

I used Slick 3.1.1 codegen to generate a Tables.scala from a MySQL (MariaDB) schema.

Tables.scala begins with this:

// AUTO-GENERATED Slick data model
/** Stand-alone Slick data model for immediate use */
object Tables extends {
    val profile = slick.driver.MySQLDriver
} with Tables

What's the best way to use these classes? As per the Slick documentation:

The file contains an object Tables from which the code can be imported for use right away. ... The file also contains a trait Tables which can be used in the cake pattern.

... I've tried variations on this example

import Tables._
import Tables.profile.api._
import slick.jdbc.JdbcBackend

class Test(s: String)(implicit db: Database) {
    def exec[T](action: DBIO[T])(implicit db: Database): T =
            Await.result(db run action)
    def run: Unit = exec(((ATable filter (_.id)).result)
}
object Test {
    implicit val db = Database.forURL(url, user, password)
    new Test("")
}

I get a compile error wherever I reference the class ATable:

could not find implicit value for parameter tables: Tables

I don't even see tables in Tables.scala. How do I get everything I need in scope to use my generated Slick classes?

1

There are 1 answers

0
Douglas Myers-Turnbull On BEST ANSWER

I got the example to work: Tables._ and Tables.profile.api._ just need to be imported inside the class with an implicit Database available.

import slick.jdbc.JdbcBackend

class Test(s: String)(implicit db: Database) {
    import Tables._
    import Tables.profile.api._

    def exec[T](action: DBIO[T])(implicit db: Database): T =
            Await.result(db run action)
    def run: Unit = exec(((ATable filter (_.id)).result)
}
object Test {
    implicit val db = Database.forURL(url, user, password)
    new Test("")
}