Unique ID Generation

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.

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?

@Bobby for number type properties, “Maximum” returns the maximum value from all properties in the collection.

Best regards, Victor.

Ok, that’s what I thought.

That could technically return the a duplicate result too then couldnt it?

For what it’s worth ive already found a solution to my problem but if anyone comes back and reads this, just to have some clear info :slight_smile:

My solution was; since I made “username” a unique field i just took the logged in users - username and added a RAND number to it.

Well, if you add +1 to Maximum - how will it return duplicate result? :slight_smile:
I’m not talking about “race” conditions, when 2 users are added at the very same moment of time. Usually this is quite rare case in most of scenarios.

Great that you’ve chosen the solution which suits your needs best!

Best regards, Victor.

Cant the maximum be reduced if the number of users are reduced? Or is this a count of all users that have ever been created?

It’s not a count, it is maximum.
E.g. you have a collection with ID property, ID is numeric.
You add a record and set ID to 999.
You add another record and set ID to 1.
When you use “Count”, you’ll get the value of 2.
When you use “Maximum”, you’ll get the value of 999.

If every time you add a new record, you put ID as Collection → ID → Maximum +1, then you will always have a higher value in a new record.
It works with all collections, not just Users.

There is a slim chance that 2 app users will add records to the collection at the same time. With this you may end with two similar IDs. But again, the chance is quite low.

I’d say that personally I’d go with unique ID generated based on 2 large random numbers :slight_smile:

Best regards, Victor.

1 Like

What do you mean by not allowed?

It works on my app with no issues creating those IDs.

Sorry, not allowed for my application. I can only have alphabetical and numerical characters for what I use the ID for. Adding the date and time field includes things like colons.

If you set a date on a non date field, it will be stored as a number…

No, it still adds the colon as well as hyphens.

image

Even if it’s a numeric field? I’m not in front of my computer but I know that’s how I do it and it’s definitely numeric

So, yeah. I guess if the field is a number field it puts out a number, but has a period in it and that is not a character I can use.

The formula I use is used on a number field.

By multiplying Current Time by 1000000 and using the round function, you end up with an integer with no decimals.

Then adding the random number between 1 and 1000, and you have a unique ID.

The formula works and the result is an integer with no decimals.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.