title: Foreign key in main database referencing attached database
body: <p>Is there any way in SQLite3 to have a foreign key in the main database that references columns in an attached database (or vice-versa?)</p>
  
  <p>I hope to share the attached (read-only) database between multiple processes, each of which has its own (read/write) main database.</p>
  
  <p>I create the parent table like this (in database 'ParentDB'):</p>
  
  <pre><code>create table Parent (id integer primary key);
  </code></pre>
  
  <p>Now I try this in the main database:</p>
  
  <pre><code>attach 'parent.sqlite3' as ParentDB;
  create table Child (id integer not null references Parent (id),
                      constraint PK_Child primary key (id));
  insert into ParentDB.Parent (id) values (42);
  </code></pre>
  
  <p>When I try it it creates the foreign key with no errors. Now I try to insert a row into the child table:</p>
  
  <pre><code>insert into Child (id) values (42);
  </code></pre>
  
  <p>And I get this error:</p>
  
  <pre><code>Error: no such table: main.Parent
  </code></pre>
  
  <p>So it seems it always assumes the parent and child tables belong to the same database.</p>
  
  <p>Also the foreign key syntax <a href="https://stackoverflow.com/a/6704466/12048">does not allow you to specify which database the parent table belongs to</a>.</p>
  
  <p>Is there a workaround?</p>
  
  <hr>
  
  <p><a href="https://stackoverflow.com/q/6703186/12048">This question</a> is related, but here both the parent and child tables are in the same attached database, whereas I have them in separate databases.</p>