0

I have been reading questions on here from people having the same problem as me and not finding any sufficient answers. I modified my application.py with the following code as per recommendations on answers to others' questions:

# configure CS50 Library to use SQLite database
db = SQL("sqlite:///finance.db")
db.execute("PRAGMA foreign_keys = ON")

I'm not sure what this actually does as it changes nothing in phpLiteAdmin that I can tell. I get no option to make a column a foreign_key.

1 Answer 1

0

What exactly are you asking? It seems as if you're unclear what a foreign key is and what the command above does. So, if that's true, here's an explanation. The code enables foreign key support in the app. It doesn't create a foreign key, it simply enables support for foreign keys in the app. (More explanation and historical context in the link below.)

Simply put, a foreign key is simply a requirement that for a value to appear in a given column in one table, it must already exist in a "foreign" column in another "foreign" table. Traditionally, both columns will have the same name and setup, even though they are in different tables.

Here's a link to a more in-depth explanation: https://www.sqlite.org/foreignkeys.html If you need more info, google is your friend. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • I read the documentation you referenced above prior to posting my question. As part of CS50 Pset7, we are tasked with creating tables using the program phpLiteAdmin version 1.9.7-dev. The program does not allow you to assign foreign keys, only primary keys. Therefore, I am at a loss for how to join two tables together. Commented Mar 22, 2017 at 17:56
  • I misunderstood the lecture for this pset and thought that the JOIN command created persistent relational databases. I realized that I did not need to use a foreign key to tie the data in one database to another, only to create in my python code, a way to insert a relational value into each table tying them together. Commented Mar 27, 2017 at 4:59
  • For clarification, a join only looks for data common to two tables. Either table can contain data not in the other. A foreign key is a restriction placed on a column in one table that says that any values in that column MUST already exist in a separate column in another table. Also, it seems from your last comment that you might be confusing tables with databases. A table is a component in a database. A database can have multiple tables, and there can be multiple databases within a given implementation or they can be spread across multiple implementations on one or more servers.
    – Cliff B
    Commented Mar 27, 2017 at 5:39
  • This is what I needed to fully understand to move forward. I too was getting hung up on trying to create foreign key constraints for my tables. Now I realize that I can join the tables simply on things like username, etc.
    – Derfoozer
    Commented Oct 31 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .