Generate a Unique ID

@Victor Can I ask you to explain how to do this:

Unique ID

I’m trying to automatically assign a unique ID to each created new user. I really would like to keep it to a simple series of sequential numbers (User 1, User 2, etc).

Your idea of using Maximum+1 seems to be exactly what I need, but I don’t understand how to implement it. Please explain as you would to a child :grinning:

Also, although you mentioned that it would be slightly possible for two user who signed up at exactly the same time to have the same ID number, that seems highly unlikely. Nevertheless, is there a way to implement a check to make sure the new ID is not already in use?

Hey Mike, there’s several ways to do this. And Victor is probably the master of all.

In my case I simply apply an action to the user sign up form.

  • In user collections add a new number field, called Unique User ID

  • On sign up forum for users, set an automatic field on the form itself, Select Users > Count (then type) +1. That will check how many users are on your app currently, and type +1 to that number to make their ID.

This could potentially cause you issues if you delete a user, which would mess the count… you could add a function Rand also. To provide a lot more protection against duplicating. See image below.

1 Like

You can set dynamic numbers in your Rand function in many different ways. You can also add in current date and time, with no formatting which is probably one of the safest also… There’s lots of forum posts on this so have a little look around. PragmaFlow has a button component that can access the Unique ID Adalo creates automatically for a collection record…

1 Like

This is very helpful. Thanks so much!

Question: @Victor suggests using ‘maximum’ instead of ‘count’ because it will add 1 to the highest number in the UserID column rather than adding to the total count of users. As I understand it, that would mean if the highest UserID currently in the User collection is User7, the next user would be assigned 8, even if some of the earlier users had deleted their accounts. Is this correct and do you agree that this would be the better approach?

1 Like

Hi,
I had that discussion sometime ago with Victor, cannot find the thread now though, yes, it is definitely recommended that you use MAX instead of COUNT, but this has a flaw still (Which we both failed to see at that time), which could be very rare to happen but that messes up everything (Or let me know does not make sense, it’s Sunday, I am braindead :smiley: ):

image

I have 8 Users currently, let’s imagine I delete USER 5:

We still have max USERS 8, so the next ID should be 9

But if I delete the highest number user (8 also in this case), in this scenario now I have 6 users, the max number will be in this case 7, so your next user would be again 8, which will not match the “internal Adalo id”. so the 9 User on this case would become the 8, the 10 the 9, and the logic would be destroyed.

The easiest way for now, but will consume 1 extra action, is to create a “control collection” that will add a record (just like a 1 or whatever you want), that when you add a record, you also add a record in that collection, so if then a record is deleted from the original, it will not be deleted from this “control collection” and you can always use that count. If you want to track all collections, you could do the following:

Let’s say you have 3 collections and want to have all IDs in all collections:

So we have this scenario:

  • Collection 1
  • Collection 2
  • Collection 3
  • Control Collection

When you add a record to collection 1, you add also to control as “Collection 1”, if you add for the 2 you add in the control as “Collection 2”, so you can keep record of how many records existed already for each collection.

You add an extra action to add also record to “control collection”, downside is that it needs an extra action:
image

How to calculate (For the new user, you would just use “current count” + 1)

Frontend calculation

Database

Hope it makes all sense :slight_smile:

I also have an API for this which is more bullet proof, I am building a process specifically for ID, if you and everyone wants to test, just drop me a DM with the EMAIL, for me to send the API for testing.

2 Likes

That’s a pretty good idea. But certainly complicated. I might take you up on testing your API soon, but I’m not ready yet.

Another question: would that whole problem go away if I just never delete users? I could flag deleted users as simply “deleted“, In fact, I could simply change their username to the word “deleted “, and remove any privacy information, leaving their record and ID number intact.

That would solve the whole issue, correct?

2 Likes

If you don’t mind the ID being numeric or not, you could use Current Date instead. Simply create a text field, and select Current Date, this will be non formatted, Since we can’t go back in time this will keep you safe also ensuring their ID is unique.

If you need it to be numeric, you could set up any number of equations, using max, count, rand, etc there’s a lot of ways to make the number unique.

1 Like

If you never delete the user, you should be fine with the “MAX” formula. But if you are in Europe or serve to European users the app, under GDPR, you need to delete the user if they request so, if not you risk fines.

The advantage with “MAX” is your ID will always match the Adalo id of the record as such the API.

The API is ready this week, I will launch it anyway, let me know once you want to test it. Cheers.

2 Likes

Wow, what a long conversation :slight_smile:

In my opinion:

  • if you need some custom uniqueID - you can use RAND function (may be in conjunction with current time in unformatted form) - like @iAppsNi suggested
  • if you’d like to have sequential uniqueID and you do not care that this ID should be equal to Adalo’s internal ID - you can use MAX function. @JL_LJ thanks for pointing out the potential flaw with deleting last user.
  • if you’d like to have a uniqueID which “mirrors” Adalo’s internal ID, you can go with @JL_LJ’s suggestion.
    Or, as an alternative, you can use API calls. Have a custom action to call Adalo API to Add a record to a collection, it will return a full record data after execution. Then have another custom action to Update newly-created record, and put internal ID from (available as a result of the 1st action) to public UniqueID field.
    Not sure how easy will this be to implement with Users collection - you need not just to create record, but also log-in the user. Should be possible though.

As a side note - I feel really disappointed that we have to invent some “bicycles with square wheels” in order to get very basic functionality, which is forbidden for some unknown reason. I prefer to spend time creating new solutions for my customers than to try to overcome artificially-created obstacle. It is possible, but what is the reason??

I’ve created a feature request to expose IDs to public (Make unique record IDs publicly available | Voters | Adalo), though there is not a lot of votes for it now.

Best,
Victor.

2 Likes

You actually can do this with 3 actions if we login the user immediately after creating it, since creating user happens in the backend with API, the login part has no direct influence.

You can create your own sign up form via the input fields:

3 Actions then:

  1. Using a button and custom action with a POST request to create the user with Adalo API. The “Password” field is hidden in the documenation, but you can still use it.

The response will be like this (I called my action USER CREATE):

  1. Create a custom action with PUT request to update the user with User Adalo API:

Don’t forget to create an “ID” field in the user collection, the “Adalo internal id” then “id” will be used as you seen in the screenshot to update the “External” “ID”.

On the custom action, then add the result from the previous with the “id” of the response

  1. Create a “regular” action to login the user after this:

And in the end you can add a 4 action to link the user to a “Home” url.

As a cool EXTRA, you can use the Marketplace component “Email validator” to validate and use as signup button at the same time.

hello, does this automatic field work in custom form? I didn’t find this possibility

You can add that inside the Signup action too in a custom form! :