meet
meet6d ago

_id or separate "pkey" ID

tl;dr: Is it best practice to use a custom field (generated with UUID) as primary key, or use the built in _id that is generated. Context: Right now, I have a collection of tables: users, documents, documentFragments etc. Each have their own unique "primary key", so for example, in the users table, I have a field userID, in the documents table, I have documentID, etc. In schema.ts, I'm linking together using the v.id, so for example, each document fragment has a field sourceDocument: v.id('documents'). At this point, is it even worth it to have the primary key UUID field (outside of the userID, since that is matching up with Clerk). That is, should I just remove the documentID and fragmentID field, and just use the _id that is generated? I originally had the custom field so I could "tag" them with a prefix that denoted what type, so for example: "doc_<UUID>".
2 Replies
Convex Bot
Convex Bot6d ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Clever Tagline
The provided _id field is designed to be the primary key for the document. It's not just a unique value, but it also contains encoding that tells Convex what table it's from, when the document was added, and some other stuff. You can read more about it here (footnote 5): https://stack.convex.dev/how-convex-works#footnote-label
How Convex Works
A deep dive on how Convex's reactive database works internally.

Did you find this page helpful?