3 ms·
>> So, in reality, the right solution is probably: use UUIDs for keys, and don’t ever expose them. > Can someone shed light on this? Some of the answers over
by zerkten 6y ago
>> So, in reality, the right solution is probably: use UUIDs for keys, and don’t ever expose them.
> Can someone shed light on this?
Some of the answers over at https://softwareengineering.stackexchange.com/questions/218306/why-not-expose-a-primary-key https://softwareengineering.stackexchange.com/questions/2183... cover the reasoning that I had been taught in the mid-nineties: it's an implementation detail and there are better choices to expose outside the database.
When it comes to UUIDs, these often have a usability penalty when exposed to users. There may be something better to expose to users depending on the context, but not always. It's never an absolute. Since few people now start by building their database first with relatively rigorous modeling, it's hard to avoid the outside exposure.
You should consider the situation though. In the early-2000s, there was a raft of web apps built in the .NET space that exposed GUIDs just because they were cool (I've literally heard them described as that), or devs didn't think about the implications. As with many ASP.NET abstractions at the time, these were problematic in the contexts used: Web Forms relied on POSTing everything and now your URLs weren't "hackable".
Retrofitting UUIDs is possible if you are using other keys. If you are merging databases you can introduce these to get the data merged, as long as you are OK with some level of fix-ups to the original (integer?) primary key that you've exposed to the application. Ultimately, it becomes an application problem if you have collisions in the original IDs.
- tgb 6y agoSorry, but my question is about whether UUIDs are or are not recommended for use as primary keys. The article seems to be saying both No and Yes to that question.