【发布时间】:2022-01-18 05:39:49
【问题描述】:
我正在开发 Spring Boot 并使用 Postgres DB。 我有一个要求,比如我需要在现有表中添加一个新列。
@Entity
@TypeDefs({@TypeDef(name = "EnumUserType", typeClass = EnumUserType.class)})
public class P_user {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"p_user_row_id_generator")
@SequenceGenerator(name = "p_user_row_id_generator",
sequenceName = "p_user_row_id_sequence",
allocationSize = 50)
@Column(name = "row_id")
private Long rowId;
private UUID p_Id;
private String username;
@Type(type = "EnumUserType", parameters = {
@Parameter(name = "enumClassName", value = "P_userRole")})
private P_userRole role;
private Date startTime;
private Date endTime;
//new Id tobe added
private UUID groupId;
}
我正在使用脚本来动态更改数据库中的表。
在 xyz.sql 文件中
ALTER TABLE p_user ADD COLUMN groupId UUID;
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateSqlException:
Migration xyz.sql failed
-----------------------------------------------
SQL State : 42701
Error Code : 0
Message : ERROR: column "groupid" of relation "p_user" already exists
Location : ../db/migration/xyz.sql
Line : 4
Statement : ALTER TABLE p_user ADD COLUMN groupId UUID
如果我不运行这个脚本,那么我会得到
ERROR: column "group_id" of relation "p_user" does not exist.
我怎样才能获得新列来现有实体,我也需要在 db 中更改表。
请在这里帮助我。欢迎提出任何建议! 蒂亚!
【问题讨论】:
-
猜测默认行为会将
groupId转换为snake_case,从而导致查询为select ..., group_id ...。尝试指定列名,例如,@Column(name = "groupId") -
在开发过程中,您可以在配置文件中将 DDL.update 标志设置为 true。
标签: java spring-boot spring-data-jpa flyway