Autoincrement not updating
See original GitHub issueBug report
Describe the bug
This seems to have only started occurring in the last couple of days.
When I seed a table during a migration (below), upon insertion the autoincrement id of the table does not seem to trigger.
The first two INSERT
calls to this table from the frontend client return a duplicate key error:
{
code: "23505",
details: null,
hint: null,
message: "duplicate key value violates unique constraint \"api_keys_pkey\""
}
However, upon the third insert attempt - the row is created successfully.
-- CreateTable
CREATE TABLE api_keys
(
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
team_id bigint REFERENCES teams ( id ) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
creator_id uuid REFERENCES accounts ( id ) ON DELETE CASCADE ON UPDATE CASCADE DEFAULT auth.uid( ),
token uuid NOT NULL UNIQUE DEFAULT uuid_generate_v4( ),
created_at timestamp WITH TIME ZONE DEFAULT timezone( 'utc'::text, NOW( ) ) NOT NULL,
updated_at timestamp WITH TIME ZONE DEFAULT timezone( 'utc'::text, NOW( ) ) NOT NULL,
disabled_at timestamp WITH TIME ZONE
);
-- InsertInto
INSERT INTO api_keys ( id, team_id, creator_id )
VALUES ( 1, 1, '2bc32eba-533d-42fc-9402-b12b9af79c75' );
INSERT INTO api_keys ( id, team_id, creator_id )
VALUES ( 2, 2, '8cb12f23-533d-4032-ab1d-946d530eda69' );
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Define a table with an autoincrementing
id
column, followed by twoINSERT
queries to the new table. - Execute SQL via the Supabase GUI.
- Attempt to insert a new row either from the frontend client or direct SQL connection.
- See
duplicate key value violation
first two attempts and success on the third attempt.
Expected behavior
Autoincrement should not care how the row is inserted (via Supabase GUI SQL script or remote access).
Screenshots
System information
- OS:
N/A
- Browser (if applies):
Edge
- Version of supabase-js:
"@supabase/supabase-js": "^1.13.1"
- Version of Node.js:
14
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
AUTO_INCREMENT VALUE IN TABLE_SCHEMA NOT ...
The INFORMATION_SCHEMA doesn't update to reflect recent alterations. MySQL 8.0 changed it so it only updates once every 24 hours.
Read more >MySQL Bugs: #91038: AUTO_INCREMENT does not increase ...
I use the script below to check the value of AUTO_INCREMENT: SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'temp_order ...
Read more >AUTO_INCREMENT - MariaDB Knowledge Base
An AUTO_INCREMENT column normally has missing values. This happens because if a row is deleted, or an AUTO_INCREMENT value is explicitly updated, old...
Read more >Be Careful With MySQL's auto_increment. How We Ended Up ...
All the inserts and updates were bulletproof so they couldn't corrupt the data. We even created a logging ... But that did not...
Read more >How to read and reset AUTO_INCREMENT in MySQL
Do not use the ALTER command on tables having large data. MySQL takes a long time to update the table in case the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
thanks to you i try
SELECT PG_GET_SERIAL_SEQUENCE('table', 'id');
to find right seq and then runALTER SEQUENCE <your_sequence> RESTART WITH 453
as you say and it worked thank you very muchHey @prescience-data, identity columns like
id
here are sequences, so if you insert a value manually it’ll clash with the sequence (more info here). Unfortunately, AFAIK there is no way for it to detect already existing values (unlike, say, MySQL’s auto_increment).You might consider using
DEFAULT
in place of1
and2
above, or alternatively you can sync them after seeding with:This updates the
id
sequence’s next value to 3, so following inserts should work normally.