I am using IntelliJ and the database tool. I had an issue with some data, so I deleted a row but then wanted to add a couple more...
I used the clone option:
I then modified my values and hit the commit
button.
However, the insert failed with the error:
ERROR: duplicate key value violates unique constraint "Primary Key some_table" Detail: Key (id)=(58) already exists.
I then tried via the console and inserting manually, but I get the same error. Each time I do this, the id increments... I have thousands of records, so I cannot keep clicking until my finger snaps off.
This is what id
looks like (when table is created):
id bigserial not null constraint "Primary Key some_table"
primary key
When I try to modify the table, I see id has a default value set as this:
nextval('some_table_id_seq'::regclass)
I've tried:
INSERT INTO some_table (id,...columns..) VALUES (DEFAULT,...columns...);
and
INSERT INTO some_table (...columns..) VALUES (...columns...);
but I get the same error...
I realise I could do something like running a query to get the MAX id
and then do my insert, but that seems a little ridiculous to me.
How can tailor my INSERT
so that it automatically gets the new/next id
?
For example, MSSQL has automatically handles this for you using newid()
.