我知道slick-codegen可以从数据库表中生成scala类。如果模型中的数据库中不存在表,我们可以做相反的事情吗?
您可以通过模型在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命令。
我要补充一点,光滑的创建表的功能不应用作任何类型的数据库迁移框架。