Auto increment ID in JavaScript
Is there a way to get the last greatest ID in a table and then increment off of that for the next row of data?
22 Replies
You can convert this for the js functions
given that
{ascending: false}
means descending. @Modgetting this error:
{"code":"22P02","details":null,"hint":null,"message":"invalid input syntax for type bigint: \"[object Object]1\""}
code:
lastID seems to be an array
console log lastID and verify this
oh yep, need to deconstruct it
Why would you be inserting the id, when it auto increments?
Reading a last value, then turning around and writing using it in another operation will also fail if there are multiple users.
unless they have it specified as UUID or some other type that's not number, but they seem to be using big int so well yeah it won't make much sense
no just a normal int that increments

just messing around right now trying to get my bearings straight with supabase
Normally you use an identity column attribute (available in the table UI also) to set up an auto incrementing column on every insert.
Anyway, was I correct?
given that {ascending: false}
means descending.
im pretty sure i have that? my id column has the
Is Identity
box checkedThen you don't even provide it from the API call.
ahh ok
It will always increment (note it can skip in weird cases on upserts, so you can't count on it going up just by 1, but it will always be bigger after each insert.
you can have a default value to whatever column you want and you won't need to pass it through the api call. Like your
id
in this case.oh i think i just broke it lol, deleted all my rows and did an insert with just a
message
attribute and it started the count as 10
and before deleting i tried it out and it inserted as 9 so its remembering that
That is fine, it is a separate counter set when it is created.
It has nothing to do with the number of records.
π
well thank you guys so much for the help
You at Mods so is there someone breaking the rules or spamming a channel? If not please donβt ping the Mods because if everyone was to do this it wouldnβt be good for the server.
my bad, won't happen again.