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 Using Databases in Python Meet Peewee Modeling

Chen Wang
Chen Wang
7,371 Points

table in database

When I watch this part, I'm kind of confusing about "Student", "student" and "Students".

We have a class called "Student". Therefore, "Student" is a table, right? First, I did not get the instructors' explanation about why we don't use "Students", because this table obviously contain multiple students, why it is single?

Second, since "Student" is the table name, why when we try to find the tables in the sqlite3, it shows "student"?

1 Answer

Dan Garrison
Dan Garrison
22,457 Points

Student (with a capital S) is the class that provides a 1:1 mapping to database with the Model class that was imported with Peewee. So, yes, it is essentially a table. Not calling it "Students" is more of a python convention, which states that this class should always be singular. The documentation for peewee wasn't too clear on why this was, but I would guess it has to do with the fact that this class just describes how the data should be added to the database. It hasn't actually added any students to the database yet.

students (plural and lowercase) is the dictionary list that contains some initial students. However, these students have not been added to the database yet. The add_students function will do this with the the following for loop:

def add_students():
    for student in students:
        try:
            Student.create(username=student['username'],
                           points=student['points'])
        except IntegrityError:
            student_record = Student.get(username=student['username'])
            student_record.points = student['points']
            student_record.save()

In this case, the for loop is saying to loop through the "students" list, create an instance of Student, and add it to the database. You could use something besides a lowercase, singular "student" (e.g. person) in the for loop. This just makes it a little more clear on what it is doing.