Unique ID Generation

Hi, All:

I need a way to generate a unique ID number. I was trying to do this with the randomizer, but I cannot get it to work consistently.

The app does not have any real users yet, so I can start with 1. Is there a way that anyone can think of to assign the next number to a user when signing up?

Hi @Bobby,

Just in case there is a built-in RAND function in Adalo (for text field you can access it via Custom Formula) :slight_smile:

Best regards, Victor.

2 Likes

Hi, @Victor

Wouldnt there be a chance with the RAND that you could get the same number twice?

Hi @Bobby,

Of course there will be a chance that you could get the same number.

For instance, if you use RAND(0,999999) the chance of the second number being like the 1st one is one of a million. If you add three more 9s it will decrease to one of a billion.
For the further numbers the probability will increase, of courseā€¦ but not much :wink:

If you want to be on the safe side you can combine RAND with a Current Time formatted as ā€œNoneā€ (for text IDs). The probability of 2 users registering at exact same time and getting exact same random number is pretty low :slight_smile:

Best regards, Victor.

1 Like

Yeah, I know you can make that chance VERY small, but since this number is being used to process payments it needs to be 100%

The second option just may work though :slight_smile: Let me look into that, thanks!

@Bobby By the way, there are some APIs out there which might help you to generate unique IDs. This is one of the examples: Micro ID Service - Generate unique IDs (uuid, snowflake, etc) | M3O

Best,
Victor

Yeah, I was trying to do it all inside of Adalo.

Also, as good as time idea sounded, it adds some other characters into the mix that arent allowed such as a colon and dash.

I think I found a solution though. Since iā€™ve made the app in a way that usernames are unique, iā€™ll couple that with the rand function and that should do it.

Two ideas:

1 you can add a random number to a date-time and youā€™ll get a unique number e.g. datetime+3 digit random number

2 if you donā€™t need to reference the ID immediately you can use integromat to watch for new records and then update an ID field with the actual Adalo ID.

I always use 2 if I donā€™t need to access the ID immediately after user creation. But 1 should give you a unique ID each time.

Isnā€™t it possible to simply use a formula in the database to add a +1 to the user ID column? Number the first user 00001, and let the formula take over after that?

Thatā€™s what I was hoping for, but I cant figure it out.

I put magic text as Count+1 to create new record to generate an unique ID.
Or 1000+count if you want it start from 1001.

1 Like

The count of what though?

if it is the Unique ID for user, it is the count record of user.
if it is Unique ID for other record, it is the count of that collection.

So if I understand you correctly, you use the all users count +1.

That could work, but what if a user deletes their account? Then someone can technically grab that same number, it may even start a chain reaction of duplicate numbers if a user deletes.

Hi @Bobby

I use this formula when the user registers to assign them an ID:
Screenshot 2022-01-14 at 06.44.48

Current time is already a unique value as it changes every second, by you could have the case that two or more users click on register on the exact second. Thatā€™s why I added the random function between 1 and 1,000. That significantly reduces the probability that two users will have the same ID.

I hope that helps.

1 Like

When user delete user record, maybe update all the information of the user to "deleted userā€™, but not removing the record. So, the userID by count may still work.
Deleting the record altogether will require deletion of all related records in other collectionsļ¼ˆtables).

One may devise a function/formula to get a unique ID, but I just find it even more complicated.
Just my 2 cents. Cheers.

Wow, what a long discussion! :slight_smile:

If someone prefers to use incrementing IDs, please keep in mind, that you need to use ID ā†’ Maximum +1 as a new ID, not Count.
Example reason not to use count: you create 10 users, 11th one will get ID=11. You delete 5 users, 6 are left. The 12th one will get ID=7.

The ā€œfunnyā€ part in all this topic is that Adalo already has unique IDs for every record in every collection. For some reason this information isnā€™t available in the app builder (however it is easily accessible via API). So we are kind of reinventing the wheel here.
It has been requested several times to make these IDs publicly available. I did this as well, please feel free to upvote: Make unique record IDs publicly available | Voters | Adalo

Best regards,
Victor.

2 Likes

Unfortunately the Current Time has characters in it that are not allowed.

Yeah, but then youā€™re piling up records in your database that you dont need.

That could be a good solution as well!

What does maximum track?