I'm running Postgres 9.5 and trying to create an unique constraint based on 3 fields. The problem I'm having is two of the columns can be nullable so rows with these fields as NULL are not seen to breach the unique constraint. I'm aiming for this to be a constraint as I am trying to do an update on conflict (UPSERT).
The table structure is something like this
product_id integer not null
colour text null
size text null
I found another question here where I can do something along the lines of the following
create unique index idx_1 on table (product_id, colour, size) where colour is not null or size is not null;
create unique index idx_2 on table (product_id, colour, size) where colour is null or size is null;
I'm not actually sure if this will actually work having two fields in the where clause but how can call this unique index on conflict?
Or maybe I should approach this a different way?
checkconstraint type. That's a valid way to handle complex logical constraints.