Day 4/100 (#100daysofcode)
Hey Everyone! I'm sorry that I didn't have time for the last 2 days, but I finally got back to work today.
Again, I didn't have too much time today, so I tackled a simple but important feature: the ability to submit posts. I created a simple form with a title
field and a textarea for the body. Posts are stored in a posts
table in the database. This table has 4 fields: 1 field, name
for the title, 1 field body
for the body, id
as a unique id and primary key, and user
. The latter one is a foreign key
that connects each post to the user that created it and thereby enforces the relationship between users and posts.
This is a 1-to-many relationship, meaning that there can be multiple posts per user, but not multiple users for one post. After reading through the SQLite docs I found that SQLite does support foreign key relationships, but doesn't enforce them when the foreign_keys
PRAGMA is not set. By default, the database's foreign_keys PRAGMA is OFF
. So, if you use SQLite, you should make sure to run PRAGMA foreign_keys=ON;
when creating the database.