
how to store auto increment ID in appwrite PK

Yeah, that’s not supported at the moment. You’ll have to check yourself what the last entry is an increment yourself

any recommendation

When you’re creating a new Document, first listDocuments, limit 1, ordered newest first, to get the last record created, and then create your new document with that+1

Although if it’s possible that multiple records will be created at the same time, you’ll have to have a way to handle race conditions

please also suggest the concurrency creation. if you don mind


Hey there 👋 You could create Appwrite Function to implement this logic. You could make getId
function, which would return to you a next available number, starting at 1.
This function could use locking mechanism to ensure 2 concurrent calls dont do DB queries, which prevents giving same number to 2 requests.
For example, you could have collection counter
and in there 1 document with attribute value
set to 1
.
So in theory you could simply get this document, do +1, save document, and return incremented value.
To introduce locking, make collection couterLock
, which could have just 1 boolean attribue locked
. If there is document, couter is locked. If not, not locked. You also need to create document here with custom ID. No matter what ID but you need to make sure it's same always. That way unique index wont let you create 2 locks.
With that in mind, you could do something along lines:
locked = true
# Wait to be unlocked, and lock
while locked
try
get couterLock document
sleep 1 second
catch
# Document not found - not locked
try
create couterLock document
locked = false
catch
# Cannot create lock doc, someone else already made it
sleep 1 second
# Code protected against race condition with concurrency
get counter document
increment value by 1
update counter document
# Unlock
delete couterLock document
Hope this helps 🙌

very handy approach. I am just courious, is the https://github.com/appwrite/appwrite/issues/3905 coming out soon

any sample function of this

I have seem implementation of numeric incremental and decremental for both integers and floats in our underlying database library. I ahve not yet seen any PRs in Appwrite API itself. Most likely it wont be coming in next version release, unless it's last-minute implementation.

I havent written any yet, and didnt see anyome implement it. But I did very similar approach in Appwrite when I needed to ensure locking for OAuth endpoints to get GitHub user information. That makes me confident about the approach.

okay, I will try to check this out.
Recommended threads
- Migrating from one self-hosted instance ...
I am trying to migrate from an older VM running Appwrite 1.6 to a new VM with the latest version. On my new VM running Appwrite 1.7.4 I am going to migrations...
- Requests for listing documents are rando...
Using an iOS app generated with Capacitor, the requests for listing documents are slow in some ocassions. SOmetimes they are very fast, sometimes they are very ...
- Appwrite CLI can't create TABLE"
hello everyone, i'm trying to create tables via the cli but its not working, it keep saying `NO TABLEs FOUND`, here are my json structure **profile.json** ```{...
