36
The perils of UUID primary keys in SQLite
(andersmurphy.com)
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Follow the wormhole through a path of communities !webdev@programming.dev
awesome!
but seriously who uses UUID as their primary keys? what's wrong with plain old INT. it's more predictable you can look at the integer primary key and tell which records created when relative to each other. also you can easily remember them. unlike UUID.
Using integer primary keys often creates race conditions (that result in collisions) when you have multiple database shards, so UUIDs have become the standard. (2 different webservers can create a record in 2 different database that then sync with eachother in the background). Using UUIDs for SQLite is less common though as SQLite is mostly used for small or local applications, but developers are used to UUIDs now and even consider them the standard for primary keys now so you do see them in SQLite databases. (Oh and there's some SQLite compatible sharding servers too)
Can't you just reserve X bits of the primary key to store a shard ID?
You would be inventing some style of UUID. Include a timestamp in front, so that it is sortable and you have Snowflake.
If you are owning every little part of the design in every nuance, sure. But how do you configure this in mysqll, postgres, etc etc. does your favorite framework support this easily.
wow, I didn't know we can have multiple databases for a single app/website. I assumed it wasn't possible when I learned about k8s and the teacher said there will always be one database while you can replicate your frontend/backend pods
Both setups are possible.
Lemmy sure loves swallowing my reply. To answer your question shortly is an offline first app where the user can add records and the relation during offline and then sync it later after connection is re-established.
the irony of this comment 😅
um, I am not sure if I understood this.
Let's say I want to create a record with my username and password. I will create it offline like saving it in a Json or plain text file. now when I get an internet connection back I will just send that username and password to the database. here the user isnt sending the Id so it shouldn't matter. but if the user is sending the Id then yeah I can see how it could become a problem. eg, two user can be sending the same Id 500 and will lead to race condition as someone else mentioned here.
is it correct?
Yeah if your only storing username and passwords and hoping no one uses the same username. Now consider I'm running tests on a piece of hardware and storing results in the database. I run 45 tests per unit so I can't use serial number as id, I want a way to get all results for a single unit and I have 5 testers since I'm high volume but each test takes 30 seconds.
Tester 1 and tester 4 might get same pk if offline, random IDs for each record won't work since I can't combine everything for 1 unit. This is more why you use uuids for each test
go it Thanks :D
As always, best choice is it's depends on situation
lots of people, I use them in certain applications. Not sure I’ve ever had a use case where I needed to memorize the PK of my data, but I suppose if you needed that, then a simply INT might be the choice.
As for sorting/creation related to each other, you can use UUIDv7. The first group of bits is time since epoch. So they naturally sort based on time created. Handy!
In most cases it's more useful they're not predictable, and I'm definitely not remembering private keys myself, so what's the point. You can have preservation of creation order with UUID v6 and v7.
Distributed systems where the identity isn’t provided by the DBMS need UUIDs. I don’t really get why you’d use uuids in sql, except in the case mentioned where the client is bringing its own ids