Short, Unique ID Values

I’m looking at how best to generate reasonably short but unique id values (for something similar to a ‘membership number’ for a user).

I’ve been playing around with the hashids library, which has the attractive feature of avoiding profanity in the resulting id, and I quite like how it operates - I could store a salt as a secret in the app, if I wanted, for example.

However, it needs an integer to encode to produce the id. I could use an auto-incrementing integer field from a data table but, is there any reason I couldn’t use the result from get_id() on a data table record? I know it’s actually composed of strings, but are those always guaranteed to be string representations of integers?

Is there a completely different/better way to do this?

If you’re storing these unique id values in the data table, there’s no reason it needs to represent anything real about the user, right?

You could use the server time in milliseconds since the epoch, and check for collisions.

The intention is not to store these at all. They just obfuscate an actual integer id.

is there any reason I couldn’t use the result from get_id() on a data table record?

Please don’t do this! We make no guarantees about the makeup of that string, and if (when!) it changes, we don’t want your code to break…

2 Likes

I thought I’d better ask first! I’ll stick with the auto incrementing integer.

Thanks