Kinara Nyakaru
Kinara Nyakaru

Reputation: 501

Knex(postres) - Is there a way to have a column unique only on some instances

I have a table that has 3 columns (id, name, orgId(foreign key) What I am trying to achieve is to have the column name unique if only the orgId matches

How do I define the schema?

Upvotes: 0

Views: 175

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246493

That would be a unique constraint defined on both columns:

ALTER TABLE tab ADD UNIQUE (orgid, name);

That will exclude only rows where both name and orgid are identical to an already existing row.

Upvotes: 1

Related Questions