1. Can we set up _id manually (edit that
1. Can we set up _id manually (edit that), or is it not really necessary? Should I just create it using v.id?
2. Sorry if this sounds dumb — what's the difference between "id: v.id " and "id: v.string "? Does v.id automatically create an index or something?
17 Replies
_id is generated for every document in Convex
At runtime it's just a string (still able to be checked as a valid id though in the Convex backend), but in TypeScript it's a "branded string", so it helps you make reasonably sure when writing code that you're actually dealing with an id for a specific table
v.id()
, v.string()
, etc. are all just validators. When used in your schema, they're run when a document insertion or change is attempted from a mutation. If the change doesn't validate it throws.
Eg., you have title: v.string()
, so if you tried to insert a todo with a number as the title, it would throw.got it
_id can i pass it manually or its auto generated?
auto generated
No need to ever create your own id fields in Convex
v.id doesn get auto created?
It does. Basically
_id: v.id('todos')
is there for every table you define automatically
Also _creationTime: v.number()
Those are the two current "system fields" auto defined and validated for every table. You can't change them, and Convex guarantees them, so they don't actually require validation.
Read-onlygot it. in specific scenario i should be using id: v.id if i want to write or update it ?
The id is required for patch, replace, and delete. You can also store ids for a number of reasons, like relating one document to another.
So if you have a users table and a todos table, the todo table would probably have a
userId: v.id('users')
field to connect the todo to the user that created it
v.string()
technically works too, you just lose the guarantees around id strings. So ids are best for anything that's referencing a specific document in Convex.actaully i m handle bit specific scenario i m generating id for chat on frontend , using uuid npm package
You could an id field like you were doing, just use
v.string()
. But there's a probably a way around that depending on what you're doing in the client.thanks man, it was really helpful , got alot of clarity
one last thing , let me now if its correct

Can't do _id
That's already defined
than how index uisng id ?
Don't have to, it's built in
ctx.db.get(id)
got it
But to avoid making your own ids on the frontend, you would use optimistic updates to create them: https://docs.convex.dev/client/react/optimistic-updates
And then
insertAtTop()
in the optimistic update to add the message: https://docs.convex.dev/api/modules/react#insertattop
Or maybe insertAtBottomIfLoaded
depending on the direction of your chat
You make a uuid as a temporary id when you use the optimistic update, and when the actual mutation completes, the fake id is replaced in the client by the real id generated by convexDo Optimistic Updates also work with PaginatedQueries?
They do, but it gets weird when you insert for paginated queries. That’s what the insert* helpers are for, like the one I linked above.