title: Postgres 9.2 - add conditional constraint check
body: <p>I'm using PostgreSQL 9.2 and need to add a conditional constraint on a column. Essentially I want to make sure that a column is false when two other columns have a certain value.</p>
  
  <pre><code>gid          | int_unsigned           | not null default 0
  realm        | character varying(255) | not null default ''::character varying
  grant_update | smallint_unsigned      | not null default (0)::smallint
  grant_delete | smallint_unsigned      | not null default (0)::smallint
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>alter table node_access add constraint block_anonymous_page_edit
  check (grant_update = 0 WHERE (gid = 1 AND realm = 'nodeaccess_rid'));
  </code></pre>
  
  <p>What this is supposed to do is make sure that <strong>grant_update is equal to 0 when gid is 1 and realm = nodeaccess_rid</strong>. However, I think rather than doing what I want, it's actually trying to make all columns mimic these values. In essence, it's trying to make sure that grant_update is always 0, gid is always 1, and realm is always nodeaccess_rid. The error I get is:</p>
  
  <pre><code>ERROR:  check constraint "block_anonymous_page_edit" is violated by some row
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I think this is going to have to be a function that gets triggered on update.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I added a row to the question above, and consequently updated the approved solution with a comment below.</p>