question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Autoincrement not updating

See original GitHub issue

Bug 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:

  1. Define a table with an autoincrementing id column, followed by two INSERT queries to the new table.
  2. Execute SQL via the Supabase GUI.
  3. Attempt to insert a new row either from the frontend client or direct SQL connection.
  4. 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

image

image

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:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
amirhosein5858commented, Mar 16, 2022
ALTER SEQUENCE <your_sequence> RESTART WITH 1500

thanks to you i try SELECT PG_GET_SERIAL_SEQUENCE('table', 'id'); to find right seq and then run ALTER SEQUENCE <your_sequence> RESTART WITH 453 as you say and it worked thank you very much

3reactions
soedirgocommented, Jun 3, 2021

Hey @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 of 1 and 2 above, or alternatively you can sync them after seeding with:

SELECT setval('api_keys_id_seq', COALESCE((SELECT MAX(id)+1 FROM api_keys), 1), false);

This updates the id sequence’s next value to 3, so following inserts should work normally.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found