So, I have a PostgreSQL/PostGIS DB attached to my QGIS project (MacOS, 3.10.).

I start adding a feature and cancel the process w/o saving the form. When adding another feature my primary key increments by one even though the last feature wasn't saved.

Is this avoidable? Meaning: Can I prevent the PK from being incremented when canceling a feature addition?


Go to

<QGIS> | Settings | Options... | Data Sources | Data source handling

and see if Evaluate default values is active; if so, deactivate it.

QGIS can either request the default value (as per column definition) while editing (option active), or read the default value definition and pass it (or simply NULL) at save time (option inactive)!
A nextval call to a PostgreSQL Sequence ( the default action/value for SERIAL types) will increment regardless of transaction outcome!

  • Evaluate default values was already deactivated. But if I understand you correctly what I want is not possible since nextval will be called even if I don’t save the form, right? I thought it might be possible since I also have a GeoDjango app connected to the same db and cancelling a form there does not increment the PK. – Jean Zombie Jan 9 '20 at 12:32
  • @JohnZombie yeah, as a matter of fact, I cannot seem to change the option permanently anyways. In theory, this is what you are looking for; I actually cannot reproduce the behavior, as on my machine, QGIS behaves as expected with that option turned off (default is evaluated at save time). A sequence will indeed get incremented regardless of a COMMIT or ROLLBACK; whenever the client application opens a transaction with an INSERT (nextval fires), the sequence will increment. – geozelot Jan 9 '20 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.