title: ERROR: there is no unique constraint matching given keys for referenced table "bar"
body: <p>Trying to create this example table structure in Postgres 9.1:</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE foo (
      name        VARCHAR(256) PRIMARY KEY
  );
  
  CREATE TABLE bar (
      pkey        SERIAL PRIMARY KEY,
      foo_fk      VARCHAR(256) NOT NULL REFERENCES foo(name), 
      name        VARCHAR(256) NOT NULL, 
      UNIQUE (foo_fk,name)
  );
  
  CREATE TABLE baz(   
      pkey        SERIAL PRIMARY KEY,
      bar_fk      VARCHAR(256) NOT NULL REFERENCES bar(name),
      name        VARCHAR(256)
  );
  </code></pre>
  <p>Running the above code produces an error, which does not make sense to me:</p>
  <blockquote>
  <pre><code>NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index &quot;foo_pkey&quot; for table &quot;foo&quot;
  NOTICE:  CREATE TABLE will create implicit sequence &quot;bar_pkey_seq&quot; for serial column &quot;bar.pkey&quot;
  NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index &quot;bar_pkey&quot; for table &quot;bar&quot;
  NOTICE:  CREATE TABLE / UNIQUE will create implicit index &quot;bar_foo_fk_name_key&quot; for table &quot;bar&quot;
  NOTICE:  CREATE TABLE will create implicit sequence &quot;baz_pkey_seq&quot; for serial column &quot;baz.pkey&quot;
  NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index &quot;baz_pkey&quot; for table &quot;baz&quot;
  ERROR:  there is no unique constraint matching given keys for referenced table &quot;bar&quot;
  </code></pre>
  </blockquote>
  <pre><code>********** Error **********
  
  ERROR: there is no unique constraint matching given keys for referenced table &quot;bar&quot;
  SQL state: 42830
  </code></pre>
  <p>Can anyone explain why this error arises?</p>