title: Unique constraints on multiple columns that cannot both be null
body: <p>I wanted to ask if there's a better way of modeling the following behavior in Postgres 10:</p>
  
  <pre><code>CREATE TABLE test.my_table
  (
      id   UUID PRIMARY KEY,
      id_a UUID,
      id_b UUID,
      some_shared_data JSONB,
      UNIQUE (id_a, id_b)
  );
  
  CREATE UNIQUE INDEX IF NOT EXISTS b_null_constraint ON test.my_table (id_a) WHERE id_b IS NULL;
  CREATE UNIQUE INDEX IF NOT EXISTS a_null_constraint ON test.my_table (id_b) WHERE id_a IS NULL;
  
  ALTER TABLE test.my_table
      ADD CONSTRAINT both_null_constraint CHECK (
          (id_b IS NOT NULL) OR (id_a IS NOT NULL));
  </code></pre>
  
  <p>I.e. the constraints are:</p>
  
  <ol>
  <li>Both <code>id_a</code> and <code>id_b</code> cannot be <code>null</code></li>
  <li>The combination of <code>id_a</code> and <code>id_b</code> must be unique (including cases where one of them is <code>null</code>)</li>
  </ol>
  
  <p>It feels to me the code above to set this up is not very expressive. Would people do this in another/more normalized way? I tried splitting this up in separate tables but then constraint (1.) is hard to satisfy.</p>