0

in play 2.5, I am using the slick codegenerator from com.typesafe.slick "com.typesafe.slick" %% "slick-codegen" % "3.1.1" % "compile" and it works fine: the auto-generated Slick data model is generated to the file target/scala-2.11/src_managed/slick/dao/Tables.scala.

My question: how can I access the generated Models and TableQuery objects from a Controller, let's say app/controllers/myDAOController.scala ? Any hints or a working example would be highly appreciated. Thank you!

1 Answers1

0

Just add

import Tables._

in your Controller. You will be able to use all your models.

Example: Here is your table:

create table IF NOT EXISTS "COMPANIES" ("ID" INTEGER NOT NULL,"NAME" VARCHAR NOT NULL);

You will access the generated Companies model like this:

val myCompanyName = Companies.filter(_.id === 10).map(_.name)

See a complete example here : https://github.com/slick/slick-codegen-example/blob/master/src/main/scala/Example.scala

Thomas Lehoux
  • 1,158
  • 9
  • 13
  • Did you set your build.sbt file as described here ? https://github.com/slick/slick-codegen-example/blob/master/build.sbt – Thomas Lehoux Feb 14 '17 at 08:58
  • I am using the activator UI for developing. In the controller `app/controllers/myDAOController.scala` I declare the `package controllers`. The statement `ìmport Tables._` yields an compile error: not found: `object Tables`. What would be the full path for Tables._ ? – thomas13 Feb 14 '17 at 09:04
  • The build.sbt is exactly the same as outlined in your example, wwith the only difference that I use mySQL. – thomas13 Feb 14 '17 at 09:08
  • Do you see Tables.scala file in `target/scala-2.11/src_managed/slick/`? – Thomas Lehoux Feb 14 '17 at 09:16
  • Yes, the Tables.scala file is correctly generated, and located in `target/scala-2.11/src_managed/slick/dao/` . Omitting the folder 'dao' does not help. – thomas13 Feb 14 '17 at 09:41
  • Try to put the `Tables.scala` file into `app/slick` folder for example (and change your import in Controller to `import slick.Tables._`). Or change `outputDir` value in `build.sbt` to `outputDir = ((sourceDirectory in Compile).value).getPath` – Thomas Lehoux Feb 14 '17 at 11:12