3 ms·
UUIDs also have a nice benefit of it being impossible to query the wrong table with one if you mixup what an FK goes to
by JamesSwift 4mo ago
UUIDs also have a nice benefit of it being impossible to query the wrong table with one if you mixup what an FK goes to
- pyuser583 4mo agoYeah this is nice - also helps with grepping dump files.
- mamcx 4mo agoHow is this done?
- sudoshred 4mo agoStatistically impossible to inadvertently generate a collision using UUID keys. UUID is designed to be unique when generated across any computer system. Practically speaking if you have an exactly matching pair of UUIDs from disparate system you have found the exact record match. The name gives a hint "Universally unique identifier". -Not a cryptographer.
- usrnm 4mo agoIt definitely is possible, just very improbable
- echoangle 4mo agoThat’s probably what’s meant by statistically impossible.
- ErroneousBosh 4mo agoIt definitely is possible, just very much a "woah, shit, guys come and look at this!" moment.
- beagle3 4mo agoMore like a moment that the guys can’t come because each one was independently struck by a lightning.
- pavo-etc 4mo ago"very" is underselling it
- 1659447091 4mo agoYou might find this thread interesting. UUIDv4 should probably be avoided https://news.ycombinator.com/item?id=48060054 https://news.ycombinator.com/item?id=48060054
- nickpeterson 4mo agoThey just mean you catch incorrect joins more easily because there is usually no overlap in keys between unrelated tables. Using int, you’re usually going to have some shared values between two unrelated tables.
- deleted 4mo ago[deleted]
- masklinn 4mo agoThe U means if you join the wrong table your join will always come up empty. It does not actually make it impossible to query the wrong table it just tells you quickly when you’ve done so.
- chrismorgan 4mo agoYou can achieve this with numeric sequences too, by having a consistent step and unique offset in all your sequences. For example, if you will never exceed 16 types, reserve four bits as the type discriminant. (You don’t have to use powers of two, but it may be convenient.) All sequences use step 16. Type A has discriminant/offset 0, yielding IDs {0, 16, 32, 48, 64, …}. Type B has discriminant/offset 1, mapping to IDs {1, 17, 33, 49, 65, …}. All the way up to Type P with discriminant/offset 15 and IDs {15, 31, 47, 63, 79, …}. This is also trivially invertible so that you can determine the type from the ID. A more common approach is to make IDs opaque strings and put a type prefix—A0, B12, P34, that kind of thing. But this way you can keep it as a number, if you wish.
- throwawayo2oe 4mo agoAlternatively just use a shared sequence for all tables.
- sgarland 4mo agoOr just write tests, instead of relying on statistical improbability to prevent disaster.
- deleted 4mo ago[deleted]