Ama*_*ugh 3 scala playframework slick slick-codegen
我知道 slick-codegen 可以从数据库表生成 scala 类。如果模型数据库中不存在表,我们可以做相反的事情来创建表吗?
您可以从模型在 Slick 中创建表:不过,它与 codegen 工具无关。
当您在 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
))
Run Code Online (Sandbox Code Playgroud)
但是,这不是自动的:您需要在启动代码中编写一些内容来决定是否运行 DDL 命令。