conditional unique constraint

I want each person to have only one primary phone number, but also have as many as they want non-primary phone numbers
export const personPhone = pgTable(
  'person_phone',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    isPrimary: boolean('is_primary').default(false),
    personID: uuid('person_id')
      .references(() => person.id, { onDelete: 'cascade' })
      .notNull(),
    phoneID: uuid('phone_id')
      .references(() => phone.id, { onDelete: 'cascade' })
      .notNull(),
  },
  (table) => ({
    // pk: primaryKey({ columns: [table.personID, table.phoneID] }),
    // uniquePrimary: unique().on(table.isPrimary, table.personID),
  })
);
Was this page helpful?