I want to add a NOT NULL contstraint to one of my table fields but only when another field has a certain value.
So I have a services table:
services:
- id
- professional_id
- is_master
I want to write a SQL constraint that whenever is_master is false professional_id cannot be null. I've tried things like:
CREATE TABLE "services" (
"id" text NOT NULL,
"professional_id" text REFERENCES professionals ON DELETE CASCADE ON UPDATE CASCADE,
EXCLUDE USING gist (
professional_id WITH =,
) WHERE (NOT is_master),
"is_master" boolean NOT NULL DEFAULT false,
PRIMARY KEY ("id")
);
What is the correct way to write this SQL?
check (not is_master and professional_id is not null)?