Relate Tables Instead of Repeating Data · practice
Add Foreign Keys to the Loans Table
The two IDs in a loan have clear destinations. item_id must match items.id, and member_id must match members.id. The table can record those promises instead of leaving them only in our heads.
Your project starts with two visible files. schema.sql is yours to edit and contains the table definitions. When you press Run, run_relationships.py builds a fresh practice database from your schema and prints what SQLite observes. Leave that runner unchanged. Later, connections.py and report.sql will appear when you need them for Python and SQL work.
The editor highlights .sql files as SQL. If you want to restore the starting files, use Reset project, then reopen this page. Keep in mind that Reset project removes your saved work here.
Declare both references
Open schema.sql. The five loans columns are already present. Add these two lines after returned_on TEXT, with a comma before the first new line:
FOREIGN KEY (item_id) REFERENCES items (id),
FOREIGN KEY (member_id) REFERENCES members (id)
A item_id refers to an existing items.id. The second gives member_id the same relationship with members.id.
Press Run. The first two lines under Declared references should point to the parent columns you intended. The output also reports this:
Default foreign_keys: 0
Missing member accepted: yes
That result is not a contradiction. The declarations are present, but SQLite starts with relationship enforcement off on a normal new connection. Declaration and enforcement are separate decisions. Your schema now states the relationships. Next, you will make every connection opened by your
Submit when returned_on still accepts a missing
Task
Open schema.sql and declare both relationships in loans: item_id must reference items(id), and member_id must reference members(id). Keep the five columns unchanged and keep returned_on nullable.
Press Run to inspect the declarations. Then submit the schema against fresh valid, missing-item, and missing-member rows.