Repository navigation
fix(postgres): move identity sequences past records' ids - #1071
Open
jayhemnani9910 wants to merge 1 commit into
Open
jayhemnani9910 wants to merge 1 commit into
jayhemnani9910 wants to merge 1 commit into
Conversation
Records that set an increment column's value left its identity or serial sequence at 1, so the next insert without an id failed with a duplicate key. Each INSERT that sets one is now followed by a setval to the column's max.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Seeding a table that has an auto-increment id with
Recordsgives SQL that loads fine, but the app's first normal insert then fails:dbml2sql --postgreswrites"id" INTEGER GENERATED BY DEFAULT AS IDENTITYandINSERT INTO "users" ("id", "email") VALUES (1, ...), (2, ...). In PostgreSQL 18,INSERT INTO users (email) VALUES ('c@x.com')then fails withduplicate key value violates unique constraint "users_pkey"because the identity is still at 1. The same happens withserial.Explicit values don't advance the identity sequence. Each INSERT that sets an
incrementcolumn is now followed by:This is what
pg_dumpdoes. It works for both identity and serial columns, and for tables in a schema.Test: a new
insert_records_incrementfixture (identity inpublic, serial in a schema). It fails on master, and the dbml-core suite passes (527 tests). I loaded its output into PostgreSQL 18: on master the next insert intousersfails andshop.ordershands out id 1. With this change they get 3 and 11.The SQL Server and Oracle exports have the same problem in a different form (they need
SET IDENTITY_INSERTandGENERATED BY DEFAULT, respectively). I left those out because I could only test PostgreSQL.AI tools assisted with this contribution.