DoogiboD
Convex Community16mo ago
2 replies
Doogibo

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);


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?
Was this page helpful?