You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

当Schema不存在时,是Spring Boot还是Flyway负责创建它?

问题:当Schema不存在时,Spring Boot还是Flyway会创建它?

我的application.properties配置如下:

spring.datasource.url=jdbc:postgresql://localhost:6432/jpa?currentSchema=jpa
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=username
spring.datasource.password=password

spring.flyway.user=username
spring.flyway.password=password
spring.flyway.schemas=jpa
spring.flyway.url=jdbc:postgresql://localhost:6432/jpa
spring.flyway.locations=classpath:db/migration
spring.main.allow-bean-definition-overriding=true

build.gradle配置:

defaultTasks 'flywayMigrate'

我在谷歌上没找到明确答案,我的初始迁移脚本里只有表创建语句,没有Schema创建语句,但每次启动应用时,Schema都会被自动创建。

更新:已确认是Flyway创建Schema

通过调试日志可以验证这一点:

2023-09-06 14:05:22.121 DEBUG 22860 --- [           main] o.f.core.internal.command.DbSchemas      : Creating schema: "jpa"
2023-09-06 14:05:22.122  INFO 22860 --- [           main] o.f.core.internal.database.base.Schema   : Creating schema "jpa" ...
2023-09-06 14:05:22.132  INFO 22860 --- [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table "jpa"."flyway_schema_history" ...
2023-09-06 14:05:22.133 DEBUG 22860 --- [           main] o.flywaydb.core.internal.parser.Parser   : Parsing  ...

Flyway默认会自动创建spring.flyway.schemas配置中指定的Schema,前提是连接数据库的用户拥有创建Schema的权限。Spring Boot本身不会主动创建Schema,除非额外配置了相关初始化逻辑(比如spring.jpa.hibernate.ddl-auto设为create或create-drop,但本次场景不属于这种情况)。

内容的提问来源于stack exchange,提问作者Jarvis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:35:02