Dynamically set next valid sequence value
Context
Summary
Inserting a new entry into one of the tables fails as the import does not correctly update the sequence counters.
Steps to reproduce
- Run the Program and expect the insert to fail on an existing Id
What is the current bug behavior?
Inserting an entity having @GeneratedValue(strategy = GenerationType.IDENTITY)
set will fail if the generated IDENTITY value is already existing.
What is the expected correct behavior?
The generated IDENTITY should be greater than the last inserted id.
Possible fixes
According to http://stackoverflow.com/questions/244243/how-to-reset-postgres-primary-key-sequence-when-it-falls-out-of-sync#answer-3698777 we can use the following statement to dynamically update the sequence:
SELECT setval(pg_get_serial_sequence('table', 'column'), coalesce(max(column),0) + 1, false) FROM table;