Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Queries Are Your Friend

Marcin Mączewski
Marcin Mączewski
4,476 Points

peewee.OperationalError: table student has no column named points

I keep getting "peewee.OperationalError: table student has no column named points" error, any ideas why? I looked through and through and couldn't figure it out.

from peewee import * 

db = SqliteDatabase('students.db')

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

    class Meta:
        database = db


students = [
    {'username': 'kennethlove',
     'points': 4888},
    {'username': 'chalkers',
     'points': 11912},
     {'username': 'joykesten2',
     'points': 7363},
    {'username': 'craigsdennis',
     'points': 4079},
    {'username': 'davefarland',
     'points': 14717},
]


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

2 Answers

I copied your code and could not reproduce the error.

The error you get indicate a problem with the students.db file. Have you tried removing it and trying again?

I also have this issue. Don't know what's wrong. Here's my code.

from peewee import *

db = SqliteDatabase('students.db')

class Student(Model): username = CharField(max_length=255, unique=True) #field that holds characters points = IntegerField(default=0)

class Meta:  #isn't a meta class
    database = db

students = [ {'username': 'liz', 'points':288}, {'username': 'chalkers', 'points':1912}, {'username': 'joy', 'points':2348}, {'username': 'peter', 'points':258}, {'username': 'davidcraig', 'points':25256} ]

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

if name == 'main': db.connect() db.create_tables([Student], safe=True) #Peewee won't break add_students()