Doogibo
Doogibo3mo ago

Optional One-To-Many Issue - Ents

Hello! First I want to say I am loving using Convex, so thank you for creating such a fantastic product with a wonderful developer experience. I have read through the issues relating to this and I just want to get final confirmation that what I'm trying to do is not currently possible, and that I am correct about the recommended approach if I am using Ents.
import { defineEnt, defineEntSchema, getEntDefinitions } from "convex-ents";
import { zodToConvex } from "convex-helpers/server/zod";
import { z } from "zod";

const schema = defineEntSchema({
requests: defineEnt(
zodToConvex(
z.object({
name: z.string(),
})
).fields
)
.edge("approvedBy", {
to: "users",
field: "approvedById",
optional: true
})
.edge("createdBy", {
to: "users",
field: "createdById",
})
.edge("requester", {
to: "users",
field: "requesterId",
}),
users: defineEnt(zodToConvex(z.object({ name: z.string() })).fields)
.edges("createdRequests", {
to: "requests",
ref: "createdById",
})
.edges("approvedRequests", {
to: "requests",
ref: "approvedById",
})
.edges("myRequests", {
to: "requests",
ref: "requesterId",
}),
});

export default schema;

export const entDefinitions = getEntDefinitions(schema);
import { defineEnt, defineEntSchema, getEntDefinitions } from "convex-ents";
import { zodToConvex } from "convex-helpers/server/zod";
import { z } from "zod";

const schema = defineEntSchema({
requests: defineEnt(
zodToConvex(
z.object({
name: z.string(),
})
).fields
)
.edge("approvedBy", {
to: "users",
field: "approvedById",
optional: true
})
.edge("createdBy", {
to: "users",
field: "createdById",
})
.edge("requester", {
to: "users",
field: "requesterId",
}),
users: defineEnt(zodToConvex(z.object({ name: z.string() })).fields)
.edges("createdRequests", {
to: "requests",
ref: "createdById",
})
.edges("approvedRequests", {
to: "requests",
ref: "approvedById",
})
.edges("myRequests", {
to: "requests",
ref: "requesterId",
}),
});

export default schema;

export const entDefinitions = getEntDefinitions(schema);
A request needs to have a "requester" and a "createdBy"; however, the approvedBy should be null until a future state when it is approved. I feel like this is a fairly common scenario. The error is Edge "approvedBy" in table "requests" has too many potential inverse; but what the problem really is, is that I cannot add optional. It sounds like the recommendation is to turn it into a many-to-many relationship, which enabled 0-many, even though in this case it doesn't really make sense. Maybe back to the drawing board on the model to find another creative way around this. Thoughts?
2 Replies
Convex Bot
Convex Bot3mo 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!
ampp
ampp3mo ago
Im basically doing this like:
(userSessions).edge('userSessionData', {
to: 'userSessionData',
ref: 'usId',
optional: true,
})

(userSessionData).edge('userSessions', { to: 'userSessions', field: 'usId' })
(userSessions).edge('userSessionData', {
to: 'userSessionData',
ref: 'usId',
optional: true,
})

(userSessionData).edge('userSessions', { to: 'userSessions', field: 'usId' })
there are some other tricks but i this should be right, getting the directional part can be weird. I have a lot of edges but also a lot of tradional ids that can be null

Did you find this page helpful?