title: (PostgreSQL) "Advanced" Check Constraint Question
body: <p>I use PostgreSQL but am looking for SQL answer as standard as possible.</p>
  
  <p>I have the following table "docs" --</p>
  
  <pre><code>    Column  |          Type          | Modifiers              
  ------------+------------------------+--------------------
   id         | character varying(32)  | not null
   version    | integer                | not null default 1
   link_id    | character varying(32)  | 
   content    | character varying(128) | 
  Indexes:
      "docs_pkey" PRIMARY KEY, btree (id, version)
  </code></pre>
  
  <p>id and link_id are for documents that have linkage relationship between each other, so link_id self references id.</p>
  
  <p>The problem comes with version. Now id is no longer the primary key (won't be unique either) and can't be referenced by by link_id as foreign key --</p>
  
  <pre><code>my_db=# ALTER TABLE docs ADD FOREIGN KEY(link_id) REFERENCES docs (id) ; 
  ERROR:  there is no unique constraint matching given keys for referenced table "docs"
  </code></pre>
  
  <p>I tried to search for check constraint on something like "if exists" but didn't find anything.</p>
  
  <p>Any tip will be much appreciated.</p>