The Slick code generator is a convenient tool for working with an existing or evolving database schema. It can be run stand-alone or integrated into your sbt build for creating all code Slick needs to work.
By default, the code generator generates
Table classes, corresponding
TableQuery values, which
can be used in a collection-like manner, as well as case classes for holding complete
rows of values. For tables with more than 22 columns the generator automatically switches
to Slick‘s experimental
HList implementation for overcoming Scala’s tuple size limit.
Parts of the generator are also explained in our talk at Scala eXchange 2013.
To include Slick’s code generator use the published library. For sbt projects add following to your build definition -
For Maven projects add the following to your
Slick’s code generator comes with a default runner that can be used from the command line or from Java/Scala. You can simply execute
and provide the following values
uri: Config URL and/or fragment for path in typesafe config, e.g.
profile: Fully qualified name of the profile class, e.g.
jdbcDriver: Fully qualified name of the JDBC driver class, e.g.
url: JDBC url, e.g.
outputFolder: Place where the package folder structure should be put
pkg: Scala package the generated code should be places in
user: database connection user name
password: database connection password
By default, the code generator places a file
Tables.scala in the given folder in a subfolder corresponding
to the package. The file contains an
object Tables from which the code
can be imported for use right away. Make sure you use the same profile.
The file also contains a
trait Tables which can be used in the cake pattern.
When using the generated code, be careful not to mix different profiles accidentally. The default
object Tablesuses the profile used during code generation. Using it together with a different profile for queries will lead to runtime errors. The generated
trait Tablescan be used with a different profile, but be aware, that this is currently untested and not officially supported. It may or may not work in your case. We will officially support this at some point in the future.
The generator can be flexibly customized by overriding methods to programmatically generate any code based on the data model. This can be used for minor customizations as well as heavy, model driven code generation, e.g. for framework bindings in Play, other data-related, repetitive sections of applications, etc.
This example shows a customized code-generator and how to setup up a multi-project sbt build, which compiles and runs it before compiling the main sources.
The implementation of the code generator is structured into a small hierarchy of sub-generators responsible for different fragments of the complete output. The implementation of each sub-generator can be swapped out for a customized one by overriding the corresponding factory method. SourceCodeGenerator contains a factory method Table, which it uses to generate a sub-generator for each table. The sub-generator Table in turn contains sub-generators for Table classes, entity case classes, columns, key, indices, etc. Custom sub-generators can easily be added as well.
Within the sub-generators the relevant part of the Slick data model can be accessed to drive the code generation.
Please see the api documentation for info on all of the methods that can be overridden for customization.
Here is an example for customizing the generator: