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

Python

What is the underlying database structure in this section?

This part is pretty hard to understand. But I'm sure that this ORM uses its underlying Sqlite database structure. Could you help me figure that out? We have a User table, and the relationship table. The User table has a primary key: 1, 2, 3 ... for each User. The relationship table has two columns, and each points to a row in User table. For example, a row for (1, 3) means that a user 1 follows a user 3, but that doesn't mean that a user 3 follows user 1 unless the relationship table has (3, 1) entry.

If you log in as a user 1, you will search all the entries in the relationship table with 1 in the first column, i.e. (1, 3), (1, 4), (1, 10),..., (1, 56) if you want to list who you follow. Conversely, you will search entries with 1 in the second column to list who follows you, i.e. (2, 1), (3, 10, (5, 1), ..., (43, 1).

Is this what Kenneth is doing in models.py?

It is for "build the social network with flask".

Can you link to a specific video or challenge?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Good question! Yes, the underlying DB is sqlite as shown in an earlier video: DATABASE = SqliteDatabase('social.db')

The models in models.py become the tables, each instance of a model is a row in the tables, and each model attribute becomes a column in the table. Plus the index column in each table (or column designated to be the index). In the Relationship table, Kenneth establishes the index to be made up from the data in the other two columns.

Depending on which query being used, the .where will filter the rows by the value being equal to self id.

So you are correct, in that, if I am user 1, and if looking for my relationship to others, it will search the from_user column to my relationships, then use the to_user to collect the results.

I purposely avoided using "first column" "second column" terms as its not always clear on the column order. Also, it isn't the indexes that are sorted and searched in the query. The index values are there to prevent duplicate entries by making them unique.

Post back if you have more questions!