Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4035322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:05:00+00:00 2026-05-20T12:05:00+00:00

I have an sqlite database structured as follows: CREATE TABLE IF NOT EXISTS Patient

  • 0

I have an sqlite database structured as follows:

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 )
);

I insert a Patient with Id #1

then I run:

INSERT INTO Event (PatientId, DateTime, EventTypeCode) VALUES (1, '2011-01-23 19:26:59', 'R')

which works

then I run:

INSERT INTO Reading (PatientId, DateTime, EventTypeCode, Value) VALUES (1, '2011-01-23 19:26:59', 'R', 7.9)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:05:00+00:00Added an answer on May 20, 2026 at 12:05 pm

    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

    1. Reading.PatientId references Event.PatientId so that the complete composite key from Event is referenced by Reading or,
    2. Add an EventId auto-increment, primary key to the Event table and use that as the foreign key in the Reading table (so that you only have EventId and Value under Reading and you can get the PatientId, DateTime, EventTypeCode out of Event).

    I’d suggest #2 so that you can avoid the redundancy of PatientId, DateTime and EventTypeCode in both Event and Reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.