I have a table with this layout:
CREATE TABLE Favorites (
FavoriteId uuid NOT NULL PRIMARY KEY,
UserId uuid NOT NULL,
RecipeId uuid NOT NULL,
MenuId uuid
);
I want to create a unique constraint similar to this:
ALTER TABLE Favorites
ADD CONSTRAINT Favorites_UniqueFavorite UNIQUE(UserId, MenuId, RecipeId);
However, this will allow multiple rows with the same (UserId, RecipeId), if MenuId IS NULL. I want to allow NULL in MenuId to store a favorite that has no associated menu, but I only want at most one of these rows per user/recipe pair.
The ideas I have so far are:
-
Use some hard-coded UUID (such as all zeros) instead of null.
However,MenuIdhas a FK constraint on each user’s menus, so I’d then have to create a special "null" menu for every user which is a hassle. -
Check for existence of a null entry using a trigger instead.
I think this is a hassle and I like avoiding triggers wherever possible. Plus, I don’t trust them to guarantee my data is never in a bad state. -
Just forget about it and check for the previous existence of a null entry in the middle-ware or in a insert function, and don’t have this constraint.
I’m using Postgres 9.0. Is there any method I’m overlooking?
Postgres 15 or newer
Postgres 15 adds the clause
NULLS NOT DISTINCT. The release notes:With this clause
nullis treated like just another value, and aUNIQUEconstraint does not allow more than one row with the samenullvalue. The task is simple now:There are examples in the manual chapter "Unique Constraints".
The clause switches behavior for all keys of the same index. You can’t treat
nullas equal for one key, but not for another.NULLS DISTINCTremains the default (in line with standard SQL) and does not have to be spelled out.The same clause works for a
UNIQUEindex, too:Note the position of the new clause after the key fields.
Postgres 14 or older
Create two partial indexes:
This way, there can only be one combination of
(user_id, recipe_id)wheremenu_id IS NULL, effectively implementing the desired constraint.Possible drawbacks:
(user_id, menu_id, recipe_id). (It seems unlikely you’d want a FK reference three columns wide – use the PK column instead!)CLUSTERon a partial index.WHEREcondition cannot use the partial index.If you need a complete index, you can alternatively drop the
WHEREcondition fromfavo_3col_uni_idxand your requirements are still enforced.The index, now comprising the whole table, overlaps with the other one and gets bigger. Depending on typical queries and the percentage of
nullvalues, this may or may not be useful. In extreme situations it may even help to maintain all three indexes (the two partial ones and a total on top).This is a good solution for a single nullable column, maybe for two. But it gets out of hands quickly for more as you need a separate partial index for every combination of nullable columns, so the number grows binomially. For multiple nullable columns, see instead:
Aside: I advise not to use mixed case identifiers in PostgreSQL.