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

Gilang Ilhami
Gilang Ilhami
12,045 Points

I got an error that says column username is not unique

  File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/peewee.py", line 115, in rera
    raise value.with_traceback(tb)                                                               
  File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/peewee.py", line 2780, in exe
    cursor.execute(sql, params or ())                                                            
peewee.IntegrityError: column username is not unique 

here are the codes

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': 'kennnethlove',
     'points': 4888}, 
    {'username': 'chalkers',
     'points': 11912}, 
    {'username': 'joykester',
     'points': 7363}, 
    {'username': 'craigsdennis',
     'points': 4079}, 
    {'username': 'davemcfarland',
     'points': 14717}

def add_students():
    for student in students:

if __name__== '__main__':
    db.create_tables([Student], safe=True)

1 Answer

Did you forget to make the username unique?

:point_right: Since you didn't post your code, I am just going to guess this:

I think peewee is complaining that you forgot to make the username field unique. I may be wrong, however, since I'm no peewee expert (in fact I might say I'm better at Flask then Peewee). If I am right (crossing fingers), though, then you should be able to add unique=True into the parentheses and get your program running.

Your code for initializing the username field should look like this:

username = CharField(max_length=255, unique=True)

As you see, I added unique=True into CharField.

I'm going to repeat: THIS MIGHT NOT WORK, I'M NO EXPERT AT PEEWEE (sorry If it looks like I'm yelling, I'm just making the text stand out *winks*)

I hope this helps. ~Alex


Gilang Ilhami
Gilang Ilhami
12,045 Points

Hey Alex, thank you very much for your help. I've edited the question, if you don't mind, please take a look :)