Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Creating and Modifying Database Tables Creating and Modifying Database Tables Creating the Tickets Table

Kareem Jeiroudi
Kareem Jeiroudi
14,984 Points

Anyone had the problem that it kept adding the values 500 and -10, even after setting `PRAGMA FOREIGN_KEYS = on;`

There's my script, and whenever I'd run, it'd insert the last two rows, although it's not supposed to.

-- enter a query
PRAGMA FOREIGN_KEYS = ON;
DROP TABLE IF EXISTS TICKETS;
CREATE TABLE IF NOT EXISTS TICKETS (
  ID INTEGER PRIMARY KEY AUTOINCREMENT,
  CONCERT_ID SMALLINT REFERENCES CONCERTS(ID),
  TICKET_HOLDER_ID INTEGER REFERENCES TICKET_HOLDERS(ID)
);
INSERT INTO TICKETS VALUES (NULL, 1, 3);
INSERT INTO TICKETS VALUES (NULL, 8, 2);

INSERT INTO TICKETS VALUES (NULL, 5, 5000);
INSERT INTO TICKETS VALUES (NULL, -10, 4);
SELECT * FROM TICKETS;

4 Answers

Sam Harris
Sam Harris
4,883 Points

I had same Error: foreign key mismatch - "TICKETS" referencing "CONCERTS" issue.

For me the issue was that the ID for CONCERTS was not a PRIMARY KEY. If you go to CONCERTS table you should see PK at ID column, if not then the ID is not a PRIMARY KEY.

I fixed it by just DROPPING CONCERTS table and recreating it with the ID as SMALLINT PRIMARY KEY

That worked perfectly, thanks!

I am having a similar problem, but I skipped inserting the invalid values and went straight to the correct values. I get the same foreign key mismatch error. I don't see anything about the answer when I search the knowledge database. Can someone please help? I am stuck. Thank you.

Steven Parker
Steven Parker
231,248 Points

It is possible that those values exist in the other tables?

Kareem Jeiroudi
Kareem Jeiroudi
14,984 Points

You mean that 5000 and -10 indeed exist in the TICKETS table?

Steven Parker
Steven Parker
231,248 Points

Well, -10 in CONCERTS, and 5000 in TICKET_HOLDERS.

When I run this script, I get:

Error: foreign key mismatch - "TICKETS" referencing "CONCERTS"

But then the default CONCERTS table in the playground doesn't have a primary key. Try identifying the key column or rebuilding the table to have a key.

Steven, I am having the same error as well. If anyone knows the answer, please let me know. Thanks