title: postgresql: foreign key to either tableA or tableB
body: <p>I'm trying to figure out how to define the schema of a database on with Postgresql 8.</p>
  
  <p>I have 2 tables:</p>
  
  <p><code>Journals</code>, <code>Books</code></p>
  
  <p>that define the publications I have</p>
  
  <pre><code>Journal:
  id_j, name, issn, other fields
  
  Book:
  id_b, name, isbn, author, other fields
  </code></pre>
  
  <p>and I have another table <code>Scans</code> that logically refers both the previous tables.</p>
  
  <pre><code>Scans:
  id, medium, source, status
  </code></pre>
  
  <p>each <code>Journal</code> or <code>Book</code> can have more than one <code>Scan</code>, but each <code>Scan</code> can refer only one <code>Journal</code> or <code>Book</code>.</p>
  
  <p>To formalize this, my first idea was to put two foreign keys in <code>Scans</code> like </p>
  
  <pre><code>Scans:
  id, medium, source, status, id_j, id_b
  </code></pre>
  
  <p>and fill in either <code>id_j</code> or <code>id_b</code></p>
  
  <p>but this solution seems to me a bit weird.</p>
  
  <p>I don't want (if it is possible) to define the table in a way like this:</p>
  
  <pre><code>Scans:
  id, medium, source, status, id_other_table, other_table_name
  </code></pre>
  
  <p>because I would like to have a formal connection between the tables.</p>
  
  <p>Any idea?</p>