title: Can I have a foreign key to a parent table in PostgreSQL?
body: <p>I'm using inheritance and I ended up having a problem.</p>
  
  <p>If I run:</p>
  
  <pre><code>select count(*) from estate_properties where id = 86820;
  </code></pre>
  
  <p>I get 1.</p>
  
  <p>But when I try to run this:</p>
  
  <pre><code>insert into property_images (binary_image, name, property_id) values (16779, 'IMG_0096.jpg', 86820)
  </code></pre>
  
  <p>I get:</p>
  
  <blockquote>
    <p>********** Error **********</p>
    
    <p>ERROR: insert or update on table "property_images" violates foreign
    key constraint "property_images_property_id_fkey" SQL state: 23503
    Detail: Key (property_id)=(86820) is not present in table
    "estate_properties".</p>
  </blockquote>
  
  <p>Also <code>ID</code> on <code>estate_properties</code> is <code>SERIAL</code>.</p>
  
  <p>Note: Another table <code>apartments</code> inherits from <code>estate_properties</code>, and 86820 was added to it. Would that make a difference? Also why would it I still have the ID in the parent table and I can select if from there.</p>
  
  <p><strong>Edit:</strong>
  Looking more closely at the documentation:</p>
  
  <p><a href="http://www.postgresql.org/docs/9.5/static/ddl-inherit.html" rel="noreferrer">http://www.postgresql.org/docs/9.5/static/ddl-inherit.html</a></p>
  
  <p>I want to achieve this:</p>
  
  <hr>
  
  <p>5.9.1. Caveats</p>
  
  <blockquote>
    <ul>
    <li>Specifying that another table's column REFERENCES cities(name) would<br>
    allow the other table to contain city names, but not capital names.<br>
    There is no good workaround for this case.</li>
    </ul>
  </blockquote>
  
  <p><strong>EDIT2:</strong>
  Here is the declaration of the foreign key:</p>
  
  <pre><code>CONSTRAINT property_images_property_id_fkey FOREIGN KEY (property_id)
        REFERENCES estate_properties (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION
  </code></pre>