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
say turn
say turnβ€’3y ago
SELECT * FROM Table ORDER BY ID DESC LIMIT 1
SELECT * FROM Table ORDER BY ID DESC LIMIT 1
You can convert this for the js functions
await supabase.from('table').select('id').order('id', {ascending: false}).limit(1)
await supabase.from('table').select('id').order('id', {ascending: false}).limit(1)
given that {ascending: false} means descending. @Mod
β˜…π’„π’π’‚π’Šπ’“π’†β˜…
getting this error: {"code":"22P02","details":null,"hint":null,"message":"invalid input syntax for type bigint: \"[object Object]1\""} code:
const lastID = await supabase
.from('test')
.select('id')
.order('id', { ascending: false })
.limit(1);

const { error } = await supabase
.from('test')
.insert({ id: lastID + 1, message: 'test' });

console.log(JSON.stringify(error));
const lastID = await supabase
.from('test')
.select('id')
.order('id', { ascending: false })
.limit(1);

const { error } = await supabase
.from('test')
.insert({ id: lastID + 1, message: 'test' });

console.log(JSON.stringify(error));
say turn
say turnβ€’3y ago
lastID seems to be an array console log lastID and verify this
β˜…π’„π’π’‚π’Šπ’“π’†β˜…
oh yep, need to deconstruct it
garyaustin
garyaustinβ€’3y ago
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.
say turn
say turnβ€’3y ago
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
No description
β˜…π’„π’π’‚π’Šπ’“π’†β˜…
just messing around right now trying to get my bearings straight with supabase
garyaustin
garyaustinβ€’3y ago
Normally you use an identity column attribute (available in the table UI also) to set up an auto incrementing column on every insert.
say turn
say turnβ€’3y ago
Anyway, was I correct?
given that {ascending: false} means descending.
β˜…π’„π’π’‚π’Šπ’“π’†β˜…
im pretty sure i have that? my id column has the Is Identity box checked
garyaustin
garyaustinβ€’3y ago
Then you don't even provide it from the API call.
garyaustin
garyaustinβ€’3y ago
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.
say turn
say turnβ€’3y ago
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
garyaustin
garyaustinβ€’3y ago
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
silentworks
silentworksβ€’3y ago
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.
say turn
say turnβ€’3y ago
my bad, won't happen again.

Did you find this page helpful?