title: What is causing Foreign Key Mismatch error?
body: <p>I have an sqlite database structured as follows:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS Patient 
  ( PatientId INTEGER PRIMARY KEY AUTOINCREMENT );
  
  CREATE TABLE IF NOT EXISTS Event 
  ( 
  PatientId INTEGER REFERENCES Patient( PatientId ),
  DateTime TEXT,
  EventTypeCode TEXT,
  PRIMARY KEY( PatientId, DateTime, EventTypeCode )
  );
  
  CREATE TABLE IF NOT EXISTS Reading 
  ( 
  PatientId INTEGER REFERENCES Patient( PatientId ),
  DateTime TEXT REFERENCES Event (DateTime),
  EventTypeCode TEXT REFERENCES Event (EventTypeCode),
  Value REAL,
  PRIMARY KEY( PatientId, DateTime, EventTypeCode )
  );
  </code></pre>
  
  <p>I insert a Patient with Id #1</p>
  
  <p>then I run:</p>
  
  <pre><code>INSERT INTO Event (PatientId, DateTime, EventTypeCode) VALUES (1, '2011-01-23 19:26:59', 'R')
  </code></pre>
  
  <p>which works</p>
  
  <p>then I run:</p>
  
  <pre><code>INSERT INTO Reading (PatientId, DateTime, EventTypeCode, Value) VALUES (1, '2011-01-23 19:26:59', 'R', 7.9)
  </code></pre>
  
  <p>and it gives me a foreign key mismatch.  Patient Id is '1' in all cases, and the datetime and typecodes match in the 2nd and 3rd queries. I do not understand what is mismatching, but I'm a bit new to actually defining foreign keys and i do not know what I am doing wrong.</p>