© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago
Tobias

nullsNotDistinct on composite unique

I have the following table
export const customersTable = pgTable("customers", {
    id: serial('id').primaryKey(),
    first_name: text('first_name'),
    last_name: text('last_name'),
    email: text('email'),
    tenant_id: integer('tenant_id').references(() => tenantsTable.id, { onDelete: 'cascade' }).notNull()
}, (t) => ({
    unq: unique().on(t.email, t.tenant_id).nullsNotDistinct()
}));
export const customersTable = pgTable("customers", {
    id: serial('id').primaryKey(),
    first_name: text('first_name'),
    last_name: text('last_name'),
    email: text('email'),
    tenant_id: integer('tenant_id').references(() => tenantsTable.id, { onDelete: 'cascade' }).notNull()
}, (t) => ({
    unq: unique().on(t.email, t.tenant_id).nullsNotDistinct()
}));


My wish is to be able to add how many customers as possible to a specific tenant_id with null email and no constraint. But only one customer per email and per tenant should be allowed.

The above solution doesn't work and I want to be able to specifically target "nullsNotDisticint" on the email column only and not the whole composite unique key.

How can I solve this?
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

Make two columns unique (composite primary key)
Drizzle TeamDTDrizzle Team / help
2y ago
Composite Keys and Unique Record ID's per User
Drizzle TeamDTDrizzle Team / help
15mo ago
SQLite onConflictDoUpdate does not work as expected on composite primary key or unique constraint
Drizzle TeamDTDrizzle Team / help
2y ago
Composite PK
Drizzle TeamDTDrizzle Team / help
2y ago