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

Why can't I add columns to my database?

I want to add columns to my database by entering this code:

from peewee import *

db = SqliteDatabase('students.db')


class Student(Model):
    username = CharField(max_length=255, unique=True)
    points = IntegerField(default=0)
    email = CharField(max_length=255, default="test")
    password = CharField(max_length=255, default="test")

    class Meta:
        database = db

students = [
    {'username': 'chrisgunawan',
    'points': 4888},
    {'username': 'chalkers',
    'points': 11912},
    {'username': 'joykesten2',
    'points': 7363},
    {'username': 'craigdennis',
    'points': 3444},
    {'username': 'davemcfarland',
    'points': 1412},
    ]

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()

def top_student():
    student = Student.select().order_by(Student.points.desc()).get()
    return student

if __name__ == '__main__':
    db.connect()
    db.create_tables([Student], safe=True)
    add_students()
    print("Our top student right now is: {0.username}".format(top_student()))

When I run the py file, it returns an error:

sqlite3.OperationalError: table student has no column named email

Why am I not allowed to add this code?

I was just following along the lessons and decided to tinker the code a bit. Not sure why I can't run the code like this. Please advise. Thanks!

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

Can you post more of the code? Are you using Django, if so I think you need to prepend CharField with model so it would be model.CharField. So maybe it doesn't know that you are actually creating a model? Again this would be easier to debug with more code.

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Okay so seeing that you are using Peewee which is an ORM, you are importing an already created database, and referencing tables that already exist...namely Student.

So what I think you should do is delete the student.db file, so that it get's recreated with the new fields and models that you added instead of trying to reference fields that don't exist in the old db.

Ah I see, thank you sir!