温馨提示:本文翻译自stackoverflow.com,查看原文请点击:scala - Can slick automatically create tables in the database (generate SQL and execute) from the models?
playframework scala slick slick-codegen

scala - Slick可以从模型中自动在数据库中创建表(生成SQL并执行)吗?

发布于 2020-04-05 00:36:25

我知道slick-codegen可以从数据库表中生成scala类。如果模型中的数据库中不存在表,我们可以做相反的事情吗?

查看更多

提问者
Amandeep Chugh
被浏览
26
Richard Dallaway 2020-01-31 22:52

您可以通过模型在Slick中创建表:不过,它与代码生成工具无关。

在Slick中定义模型时,可以使用该.schema方法生成数据库模式命令。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
))

但是,这不是自动的:您需要在启动代码中编写一些内容来决定是否运行DDL命令。