I believe the issue occurs here. The version is incremented prior to deciding if the ID should be included. This results in the record being considered not new for purposes of including the ID.
CREATE TABLE account (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
version INTEGER NOT NULL
);
@Table
data class Account(
@Id var id: Long = 0L,
var name: String = "",
@Version val version: Long = 0L,
)
org.springframework.dao.DataIntegrityViolationException: executeMany; SQL [INSERT INTO account (id, name, version) VALUES ($1, $2, $3)]; duplicate key value violates unique constraint "account_pkey"; nested exception is io.r2dbc.postgresql.ExceptionFactory$PostgresqlDataIntegrityViolationException: [23505] duplicate key value violates unique constraint "account_pkey"