How to add a unique for a combination?

This is my access schema:
export const access = pgTable("access", {
id: text("id").primaryKey(),
role: roles("role").default("member"),
createdAt: timestamp("created_at", {
withTimezone: true,
mode: "date",
})
.defaultNow()
.notNull(),
organizationId: text("organization_id").references(() => organization.id),
userId: text("user_id").references(() => user.id),

});
export const access = pgTable("access", {
id: text("id").primaryKey(),
role: roles("role").default("member"),
createdAt: timestamp("created_at", {
withTimezone: true,
mode: "date",
})
.defaultNow()
.notNull(),
organizationId: text("organization_id").references(() => organization.id),
userId: text("user_id").references(() => user.id),

});
The combination of organizationId and userId must be unique. But only the combination. How can I achieve that? I'm using Postgresql. Thank you so much.
2 Replies
Leonardo Silveira
Drizzle ORM - Indexes & Constraints
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
jacin
jacinOP5mo ago
It wasn't working but I just realized is, obviously, my fault. I was using on() in a field, not the table. Thank you and sorry
Want results from more Discord servers?
Add your server