Hi,
I need help to figure out how to auto generate a unique user id when a user signs up to my app.
I have a user id property for each user, so I’d like each new user to have the previous user id +1.
What I’ve done so far is to attach a ‘create’ custom action on the submit button on the sign-up form referencing the ‘User ID’ property in Users collection to simply ‘+1’…but it’s not worked…and I can’t figure out why…
You can’t edit the primary key ID for a record. It’s not like a SQL db where you can reference the id of the record and do math with it.
The best way to accomplish what you’re trying to do is to set the User ID to a function being a random number between a range. So click on magic text icon then hover over functions and use the “RAND” function. Set the range from 0 to 99999999999
Well you also have 2 separate functions on the same action (Sign user up and Create User). So which one are you doing? If your form “Signs the user up” then you have to set the function immediately after it to “Update” the logged in user because you’ve already created the user in the first function. Even though it runs on the same singular action, the “Sign up” function runs first, then the “Update” function will run.
What I’ve used that worked for me was to access the maximum ID of a table and add 1 to that. So if there’s no ID, 1 is the result. now there’s only one ID, its 1 and its the maximum, so the next will be 2. If I delete the record with the ID of 1, then 2 is still the maximum and the next will be 3.
The difference with the RAND option is you will get incremented IDs instead of just random IDs. Also I think with just the RAND option there may be a far out possibility of repetition, though I’m not sure about it, but I know its not easy to set a unique fields Adalo yet.
Great, thanks guys, it worked!
Simple when you know how… lol
For those coming after who also have this problem, here’s a couple of screenshots of how it’s set up and what you should be looking for.
Create an ‘Automatic Field’ and clicking the magic symbole navigate to Users -> User ID -> Maximum and then add ‘+1’ and you’re done. (obviously you’ll have to create the ‘User ID’ property on your collection.