Core problem I'm trying to solve: I want to have ID's on my database models that are similar to Stripe's (i.e. of the form aaa_ABCD1234
where the ABCD1234
part is a ULID and the aaa_
part is effectively the table name (or a shortened version of it)). This feature has saved me a ton of time in debugging Stripe integrations and I'd love for users of my systems to be able to have those same benefits. However, I know taking the nieve approach and just a string as a primary key on a table is terrible for performance (mostly indexes and consequently joins as I understand it) so I want to take advantage of the built in UUID datatype in the DB to store only the ABCD1234
part in the DB and not store the aaa_
part since that will be identical for all rows in that table.
Where I stand today
I'm currently thinking of doing this as follows:
_db_id
?) that is the UUID and technically the primary key on the model in the DB.What I'm struggling with in this approach is how to get Django internally/with all it's automagicalness to use the generated field as the "public" primary key but use the _db_id
field when talking to the DB/making joins/etc.
Is there a solution here I'm missing? Is there a better way to do this?
So it turns out I was just way overcomplicating this entire idea. In the end I just created my own custom model field that did all of what I was trying to do above. For this specific situation I took the built-in UUID field that Django already provides and extended it with ULID verification and appending/removing the table prefix when going from Python to the DB and back.