I have one entity that already have records in database (20 records).
My primary key column is already defined as SERIAL
in a postgres database.
My entity:
@Entity
@Table(name = "foo", schema = "public")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class FooEntity {
@Id
@SequenceGenerator(name="foo_seq", sequenceName = "foo_seq", initialValue = 20, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "foo_seq")
@Column(name = "id")
private Integer id;
}
I run a query that returns all existing sequences in database and i find the "foo_seq". In my service, when i try to call my repository to save the data, its returned this error:
FooEntity fooEntity = FooEntity.builder()
.id(null)
.build();
FooEntity savedEntity = fooRepository.save(fooEntity);
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "foo_pkey" Detail: Key (id)=(5) already exists.
Seems that spring is not using the value defined in initialValue
(should try to save with id 20, but instead, is using your own increment starting with 1). When i call this method again, the increment is added, but not starting with 20.
I'm missing something? Should i have alter this sequence in postgres to start with id 20?