willemW
Convex Community2mo ago
1 reply
willem

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



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