willem
willem8h ago

Unique indexes when using Convex Ents

Hi people, Given this example schema:
const schema = defineEntSchema({
// Firms
firms: defineEnt({
name: v.string(),
description: v.optional(v.string()),
....,
})
.edges("contacts", { ref: true }) // One firm has many contacts

contacts: defineEnt({
name: v.string(),
phone: v.optional(v.string()),
industry: v.optional(v.string()),
website: v.optional(v.string()),
})
.field("email", v.string(), { unique: true })
// Each contact belongs to one firm
.edge("firm")
const schema = defineEntSchema({
// Firms
firms: defineEnt({
name: v.string(),
description: v.optional(v.string()),
....,
})
.edges("contacts", { ref: true }) // One firm has many contacts

contacts: defineEnt({
name: v.string(),
phone: v.optional(v.string()),
industry: v.optional(v.string()),
website: v.optional(v.string()),
})
.field("email", v.string(), { unique: true })
// Each contact belongs to one firm
.edge("firm")
Is it possible to have a composite unique constraint on the contacts, such that it uniques by the contact.email and the firm edge? I know I can do multiple fields for an index, but from what I can it only lets you chose fields defined on the table, not edges or foreign key relations. Basically I want to able to enforce that two different firms can have the same contact, but a single firm can only the same contact once Any insight appreciated, i've tried reading both the ents doco and db stuff and haven't been able to figure it out
1 Reply
Convex Bot
Convex Bot8h 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!

Did you find this page helpful?