Postgres auto increment keys initial value

Hey @JonL, What did you end up doing for this as a workaround?

I recently switched over a new project clone to use a PG database and after setting it all up in Wappler the PK increment ID fields are screwing up all by INSERT queries.

They were defined as Increments in Wappler but created as INT4 32 characters in Postgres and then have
nextval('departments_id_seq'::regclass) in the defaults section of that field.

I think i figured it out. Looks like Wappler did change it as per your linked suggestion.

The problem is that i imported a bunch of demo data that already had values in the ID fields. When creating a new ID, postgres thinks to start back at 1, and there is already a 1 in the ID field etc.

Will need to investigate further…

1 Like

You can manually set the initial counter. So if you have 200 ids going from one to 200 you can make PG start at 201.

https://www.postgresql.org/docs/12/sql-altersequence.html

In your example:

ALTER SEQUENCE departments_id_seq RESTART WITH 201

Make sure to read the docs as PG caches the counter in certain scenarios.

1 Like

Thanks Jon. This worked. I also emptied some of my tables and re-imported without any IDs so the system could generate its own.

A combination of these two actions fixed it up for me.

1 Like