title: PostgreSQL - Multiple constraints
body: <p>I want to add several <code>CHECK CONSTRAINTS</code> to a PostgreSQL 13 table. In natural language logic is : if a field contain a defined value, an other field must be filled. I have several scenarios to combine. When I add just one constraint it's ok, but when I want to accumulate them, <code>CONSTRAINTS</code> aren't respected and row can't be inserted.</p>
  <p>Here is my table:</p>
  <pre><code>CREATE TABLE IF NOT EXISTS demo_table
  (
      uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
      id integer NOT NULL DEFAULT nextval('demo_table_id_seq'::regclass),        
      thematic character varying COLLATE pg_catalog.&quot;default&quot;,        
      field_a character varying COLLATE pg_catalog.&quot;default&quot;,        
      field_b character varying COLLATE pg_catalog.&quot;default&quot;,        
      CONSTRAINT demo_table_pkey PRIMARY KEY (uuid),
      CONSTRAINT field_a_check CHECK (thematic::text ~~ 'A'::text AND field_a IS NOT NULL),
      CONSTRAINT field_b_check CHECK (thematic::text ~~ 'B'::text AND field_b IS NOT NULL)
  )
  </code></pre>
  <p>My expected logic is : when <code>thematic</code> like '<code>A</code>', <code>field_a</code> can't be <code>NULL</code> or when <code>thematic</code> like '<code>B</code>' <code>field_b</code> can't be <code>NULL</code>. With this settings I can't add rows because my <code>CONSTRAINTS</code> definitions never check both conditions (<code>field_a IS NOT NULL and field_b IS NOT NULL</code>).</p>
  <p>I tried to define an unique <code>CONSTRAINT</code> as suggested in this <a href="https://stackoverflow.com/questions/65551572/sql-check-constraint-with-multiple-conditions">post</a>, but <code>CHECK CONSTRAINT</code> isn't respected either because parenthesis who isolate conditions aren't saved in the definition.</p>
  <pre><code>CREATE TABLE IF NOT EXISTS demo_table
  (
      uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
      id integer NOT NULL DEFAULT nextval('demo_table_id_seq'::regclass),        
      thematic character varying COLLATE pg_catalog.&quot;default&quot;,        
      field_a character varying COLLATE pg_catalog.&quot;default&quot;,        
      field_b character varying COLLATE pg_catalog.&quot;default&quot;,        
      CONSTRAINT demo_table_pkey PRIMARY KEY (uuid),
      CONSTRAINT field_a_b_check CHECK (thematic::text ~~ 'A'::text AND field_a IS NOT NULL OR thematic::text ~~ 'B'::text AND field_b IS NOT NULL)
  )
  </code></pre>
  <p>How to combine multiple <code>CONSTRAINTS</code> like <code>(IF ... ) OR (IF ... ) OR (IF ...)</code> ?</p>