How to Use UUIDv7 in Python, Django and PostgreSQL
paulox.net
paulox.net
It gets even worse if more than one backend has the ability to create entires that need to be in precise chronological order, either inserted “immediately” to a master DB or via eventual consistency through geographically distributed backends+DBs.
Sure, you can also use separate timestamp columns, but the timestamp portion of v7 appears to be sufficient for most use cases anyhow -- why have redundant data?
I believe I have recently bookmarked a trigger for MSSQL Server that is meant to autogenerate a UUIDv7 for an identity column, will try to link here later if anyone signals interest.
Regarding the timestamp, a dedicated column generated by the database from uuid_extract_timestamp() can be very practical in Django. It is written at insert time by PostgreSQL, defined declaratively in the model with GeneratedField, and handled entirely by the ORM without relying on triggers. It also makes filtering, ordering or using the Django admin faster and simpler, since querying on a proper datetime field avoids extra annotations or computation on the UUID expression.
If you have a reference for UUIDv7 support in MSSQL I’d really appreciate it, ideally from the official documentation. I’m also curious to know from which version it is available and whether the current Django MSSQL backend exposes it already.
There isn’t any AFAIK. What I ran across was SQL that was meant to go into a trigger or something like that to generate a proper UUIDv7 as if MSSQL was creating one.
Hmmm… could have sworn it was featured here on HN, could have sworn I saw it within the last six months, could have sworn I bookmarked it for closer examination. Not finding it in my favourited stories here on HN, and I have even double-checked on Reddit and Lemmy - no bueno. Really frustrating me, because I love UUIDs for their resistance to the incrementing attacks that integer primary keys typically suffer from.
Edit: After a brief Google search this tickles my memory, but not entirely sure if it is the same code I remember looking at: https://github.com/osexpert/GuidPhantom/blob/main/Uuid_v7_v8...
So, as usual, “it depends”. ;-)
There is some discussion whether or not to add helpers for this to Python‘s uuid7 module: https://github.com/python/cpython/issues/130843#issuecomment...