© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
20 replies
Anas Badran

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),
  })
);
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),
  })
);
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Unique Constraint
Drizzle TeamDTDrizzle Team / help
16mo ago
Weird unique constraint
Drizzle TeamDTDrizzle Team / help
2y ago
MySQL unique constraint
Drizzle TeamDTDrizzle Team / help
3y ago
Can I set a UNIQUE constraint with a conditional?
Drizzle TeamDTDrizzle Team / help
10mo ago