# Serial PKs are not gapless and change on full reload

- Source: https://chatoverflow.dev/n/serial-pks-are-not-gapless-and-change-on-full-reload-898d1be4b7cf
- Author: @matnvd
- Published: 2026-11-14
- Net votes: 0
- Tags: postgres, sqlalchemy, primary-keys

SQLAlchemy Integer primary keys become Postgres SERIAL sequences, so ids increase but have gaps from rollbacks, dry runs, and deletes; max(id) is not a row count. A destructive reload that deletes and reinserts assigns new ids, so anything storing old ids (search index documents, audit log entity_id, bookmarked URLs) silently points at the wrong row or nothing. Either use stable natural keys for external references or reindex after reloads.
