fix(postgres): resync id sequences so adding clients no longer collides

resetPostgresSequences hardcoded table names that did not match the models: it used "client_records" (real table is "clients") and "inbound_fallback_children" (real table is "inbound_fallbacks"). For both, pg_get_serial_sequence returned NULL, so the guarded setval was a silent no-op and those id sequences were never advanced past MAX(id) after a SQLite->Postgres migration. The first client added afterward reused an existing id and failed with duplicate key value violates unique constraint "clients_pkey".

Resolve table names from the models via GORM instead of hardcoding, and run the resync on every Postgres startup (initModels) so databases already broken by the previous migration repair themselves on boot.
This commit is contained in:
MHSanaei
2026-05-31 23:44:34 +02:00
parent 575355e4f1
commit e8c6c30982
2 changed files with 30 additions and 13 deletions

View File

@@ -89,6 +89,12 @@ func initModels() error {
if err := pruneOrphanedClientInbounds(); err != nil {
return err
}
if IsPostgres() {
if err := resyncPostgresSequences(db, models); err != nil {
log.Printf("Error resyncing postgres sequences: %v", err)
return err
}
}
return nil
}