Why sometime ents one-to-one with optional: true work, sometimes not?
so my schema is this:
I first create wallet then I create balance
In other cases I also use this approach with optional: true and it seem to work fine
17 Replies
The field storing the edge is on the required end of the edge.
So in your case it's
walletId
stored on balance
s.ah i thought it will store Ids on both ends.
If I want relation on both ends I should use:
in balances table:
walletId: Id<"wallets">
in wallet table:
balanceId: Id<"balances">
right?
these are indexed automatically right?
The reason I need this is because I want to query/get one when I know the other and vice versa
is that the right path to think about this case?
The reason I need this is because I want to query/get oneYou don't need to store the IDs in both tables. You can traverse the edge from either side: https://labs.convex.dev/convex-ents/read#traversing-11-edge
Reading Ents from the Database - Convex Ents
Relations, default values, unique fields and more for Convex
And you couldn't make both IDs required, since you have to create one of the documents first.
I use regular querying so this should work right?
Is this an alternative or essentially only this will work?
Only this will work with the way ents model the edges.
But you can add the extra field in if you want. But then you're on the hook on keeping the fields in-sync (writes).
I'm curious how/why you're using the Ents schema but not
ctx.table
?queries like
ctx.table...
requires additional setup and I need to maintain that setup, for now regular queries and mutations works just fine,Interesting, so you find the schema valuable enough on its own (mainly for edges?)? You won't get typechecked reads for many:many edges.
Yeah, I think ents makes it nice to define schema. I've been reading some ents docs and actually thinking to migrate to
ctx.table...
thing since it brings additional benefits. Any chance to include these helpers (functions) in convex-helpers package?Which helpers would you like to see? There are some relationship helpers already in convex-helpers.
functions are required to declaredin functions.ts file to use ents querying, mutations and actions
@kstulgys I don't understand.
The
functions.ts
file name is just a suggested convention, you can put custom functions wherever you like.those functions that are inside functions.ts... I would like to import these from convex-helpers rather than maintaining it myself.
I suspect you'll find that you'll end up wanting to put more things in those functions - or making more versions of them, e.g. one that automatically checks that the logged in user is an admin. The trick with having a helpers version would be that they need to be typed to your schema, so you'd still need to define them somewhere that you can add type parameters. e.g.
const { query, mutation, ...} = entFunctions(schema)
make sense ,thank you