I’m not familiar with SQLite but a little Google’ing turned up this. The documentation says
If the database schema contains
foreign key errors that require
looking at more than one table
definition to identify, then those
errors are not detected when the
tables are created. Instead, such
errors prevent the application from
preparing SQL statements that modify
the content of the child or parent
tables in ways that use the foreign
keys. Errors reported when content is
changed are “DML errors” and errors
reported when the schema is changed
are “DDL errors”. So, in other words,
misconfigured foreign key constraints
that require looking at both the child
and parent are DML errors. The English
language error message for foreign key
DML errors is usually “foreign key
mismatch” but can also be “no such
table” if the parent table does not
exist. Foreign key DML errors are may
be
reported if:
- The parent table does not exist, or
- The parent key columns named in the foreign key constraint do not exist,
or- The parent key columns named in the foreign key constraint are not the
primary key of the parent table and
are not subject to a unique constraint
using collating sequence specified in
the CREATE TABLE, or- The child table references the primary key of the parent without
specifying the primary key columns and
the number of primary key columns in
the parent do not match the number of
child key columns.
I suspect you might be running into #3 in that list.
Also, while other DBs might support using a non-unique index as a foreign key reference, (see answers here), it’s a bad design choice in my opinion. I would restructure so that either
Reading.PatientId
referencesEvent.PatientId
so that the complete composite key fromEvent
is referenced byReading
or,- Add an
EventId
auto-increment, primary key to theEvent
table and use that as the foreign key in theReading
table (so that you only haveEventId
andValue
underReading
and you can get thePatientId, DateTime, EventTypeCode
out ofEvent
).
I’d suggest #2 so that you can avoid the redundancy of PatientId, DateTime
and EventTypeCode
in both Event
and Reading
.