【发布时间】:2021-11-25 00:53:55
【问题描述】:
在我将 Flyway 添加到我的项目之前,我可以运行 POST 请求并成功创建新用户,ID = 1,下一个 ID = 2 等等。
然后我添加了 Flyway 来创建表并通过 V1_init.sql 插入一些测试数据:
create table "user"(
id int8 not null,
username varchar(255),
);
insert into "user" values (1, 'user1');
insert into "user" values (2, 'user2');
insert into "user" values (3, 'user3');
表已创建。已插入用户。
尝试运行 POST 请求 -> 错误 500
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "organisation_pkey" Key (id)=(1) already exists.
所以我的应用应该添加 ID=4 的新用户,但它似乎无法识别已经添加了 3 个用户。
我正在使用 GenericEntity:
@Getter
@Setter
@MappedSuperclass
public abstract class GenericEntity<ID extends Serializable> implements Serializable {
@Id
@GeneratedValue
protected ID id;
}
application.properties:
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/my-app
spring.datasource.username=user
spring.datasource.password=user
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
我尝试使用所有策略@GeneratedValue,更改 spring.jpa.hibernate.ddl-auto,在 init.sql 中添加没有 id 的用户(不起作用)
但仍然没有积极的影响。有什么想法可能是错的吗?
【问题讨论】:
标签: java postgresql spring-boot crud flyway