I've understood that slick-codegen can generate scala classes from the database tables. Can we do the opposite, creating tables if they don't exist in the database from the models?
Asked
Active
Viewed 719 times
3
-
1I believe not directly, see here https://stackoverflow.com/questions/46382360/create-table-from-slick-table-definition – Ossip Jan 15 '20 at 15:48
-
Thanks @Ossip. The latest version of play at this point says the DDL plugin is removed. https://www.playframework.com/documentation/2.8.x/PlaySlickMigrationGuide#DDL-support-was-removed – Amandeep Chugh Jan 16 '20 at 14:53
1 Answers
6
You can create tables in Slick from a model: it's not related to the codegen tool, though.
When you define a model in Slick, you can use the .schema
method to generate the database schema commands. There are examples of this in The Slick Manual:
// Assuming we have coffees and suppliers queries, we combine the schemas:
val schema = coffees.schema ++ suppliers.schema
// Now we can run a variety of commands to CREATE TABLE etc:
db.run(DBIO.seq(
schema.create,
schema.createIfNotExists,
schema.drop,
schema.dropIfExists
))
However, that's not automatic: you'd need to write something in your start-up code to decide to run the DDL commands or not.

Richard Dallaway
- 4,250
- 1
- 28
- 39
-
1I would add that slick's ability to create tables should not be used as any kind of database migration framework. – CPS Jan 31 '20 at 15:14