[Convex Ents] Problems with inverse field

Hi, I want to create another edge to my table to another table which keeps track of which user read a message so I first added the edge like that:
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", { to: "messages" }),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", { to: "users" }),
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", { to: "messages" }),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", { to: "users" }),
With that I got this error:
Error: Hit an error while evaluating your schema:
Uncaught Error: Specify `table` if you're customizing the `field` or `inverseField` for a many:many edge. Config: `{"to":"messages","inverseField":"readBy"}`
Error: Hit an error while evaluating your schema:
Uncaught Error: Specify `table` if you're customizing the `field` or `inverseField` for a many:many edge. Config: `{"to":"messages","inverseField":"readBy"}`
So I added the inverseFields:
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", { to: "messages", inverseField: "readBy" }),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", { to: "users", inverseField: "readMessages" }),
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", { to: "messages", inverseField: "readBy" }),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", { to: "users", inverseField: "readMessages" }),
So now I got this error:
Error: Hit an error while evaluating your schema:
Uncaught Error: Specify `table` if you're customizing the `field` or `inverseField` for a many:many edge. Config: `{"to":"messages","inverseField":"readBy"}`
Error: Hit an error while evaluating your schema:
Uncaught Error: Specify `table` if you're customizing the `field` or `inverseField` for a many:many edge. Config: `{"to":"messages","inverseField":"readBy"}`
I tried some other stuff too but I weren't able to get it working, this is one of my attempts. Could someone tell me how I can build this thing?
8 Replies
kapa.ai
kapa.ai•7mo ago
Hi @FleetAdmiralJakob 🗕 🗗 🗙. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, it seems like you're trying to create a many-to-many relationship between users and messages tables. However, the error messages suggest that you need to specify the table if you're customizing the field or inverseField for a many:many edge. Unfortunately, the knowledge sources do not provide a direct solution to your problem. However, they do provide some insights into how to define edges and inverse edges in Convex Ents. For instance, in the Discord support thread: [Convex Ents] Missing inverse edge (https://discord.com/channels/1019350475847499849/1239185755465846864), a user had a similar issue with defining edges. The solution was to use singular forms in the edge() method and plural forms in the edges() method. However, this might not directly apply to your case as you're dealing with a many-to-many relationship and the error message suggests that you need to specify the table in your configuration. I would recommend checking the Convex documentation or reaching out to the Convex support for more specific guidance on how to define many-to-many relationships in Convex Ents. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai•7mo ago
<@779381502311137301> kapa.ai is still learning and improving, please let us know how it did by reacting below
FleetAdmiralJakob 🗕 🗗 🗙
Hi, added the tables:
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", {
to: "messages",
inverseField: "readBy",
table: "messages",
}),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", {
to: "users",
inverseField: "readMessages",
table: "users",
}),
users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", {
to: "messages",
inverseField: "readBy",
table: "messages",
}),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", {
to: "users",
inverseField: "readMessages",
table: "users",
}),
Now get this error:
Uncaught Error: Missing inverse edge in table "messages" for edge "readMessages" in table "users"
Uncaught Error: Missing inverse edge in table "messages" for edge "readMessages" in table "users"
@kapa.ai
kapa.ai
kapa.ai•7mo ago
Let me check the knowledge base for you. One moment! :books: Sorry @FleetAdmiralJakob 🗕 🗗 🗙, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
kapa.ai
kapa.ai•7mo ago
Let me check the knowledge base for you. One moment! :books: Sorry @FleetAdmiralJakob 🗕 🗗 🗙, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
ian
ian•7mo ago
Sorry we accidentally enabled Kapa.ai in #support-community but we're disabling it again - you can keep going with AI in #ask-ai. I haven't used Ents much so I'm not sure, but I think you need to define a new table for the many-to-many relationship: https://labs.convex.dev/convex-ents/schema#manymany-edges
Ent Schema - Convex Ents
Relations, default values, unique fields and more for Convex
FleetAdmiralJakob 🗕 🗗 🗙
Ok interesting, do I have to do that in this case only? Because for my other many:many relations I did not need it. Wait. I think I got it Hey, were able to fix this one. Thank you for your awesome support. Correct schema is:
const schema = defineEntSchema({
privateChats: defineEnt({})
.field("support", v.boolean(), { default: false })
.edges("users")
.edges("messages", { ref: true }),

users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", {
to: "messages",
inverseField: "readBy",
table: "readMessages",
}),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", {
to: "users",
inverseField: "readMessages",
table: "readMessages",
}),
});
const schema = defineEntSchema({
privateChats: defineEnt({})
.field("support", v.boolean(), { default: false })
.edges("users")
.edges("messages", { ref: true }),

users: defineEnt({})
.field("clerkId", v.string(), { unique: true })
.field("username", v.string(), { unique: true })
.field("firstName", v.optional(v.string()))
.field("lastName", v.optional(v.string()))
.edges("privateChats")
.edges("messages", { ref: true })
.edges("readMessages", {
to: "messages",
inverseField: "readBy",
table: "readMessages",
}),

messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user")
.edges("readBy", {
to: "users",
inverseField: "readMessages",
table: "readMessages",
}),
});
For all others in the future: the table must be a new table that is storing all the relations

Did you find this page helpful?