I want to add a secondary generated key for my tables in a Ruby on Rails application that currently uses a PostgreSQL database. If possible I’d like to be database agnostic. The goal is to use this keys on URLs so external users can’t guess the size of the database or the amount of records of each type I have.
I know about UUIDs but they are long and an overkill. Also, this field should be indexed and I understand that random data in an index is problematic.
If there’s no clean solution, I’ll just make a little formula to apply to the id and make sure I check for collisions and avoid them; but I’m sure I’m not the first one to have this problem.
I’m thinking of doing this:
where n is a safe base starting number… I’d say, 5 or 6. I filter out some characters I don’t want in the id. When there’s a database collision, I run it again but I increment n, so if a table suddenly gets a crazy amount of records, it shouldn’t take that many tries to find an available one.