mardi 2 juillet 2019

What's a best practice for saving a unique random hash to db?

I have a table with a varchar column named hash, which is supposed to hold a unique, 8-char random hash, which is going to be used as an unique identifier by users. This field should be generated and saved on creation of objects, I have a question about how to create it:

Most of recommendations point to UUID field, but it's not applicable for me because it's too long, and if just get a subset of it then there's no guarantee of uniqueness.

Currently I've just implemented a loop in my backend (not DB), which generates a hash and tries to insert it to DB, and retries if the hash turns out to be not unique. But I feel that this is just a really bad practice.

What's the best way to do this?

P. S.: I'm using Postgresql 9.6




Aucun commentaire:

Enregistrer un commentaire