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 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
ise                                                                                              
    raise value.with_traceback(tb)                                                               
  File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/peewee.py", line 2780, in exe
cute_sql                                                                                         
    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:
        Student.create(username=student['username'],
                       points=student['points'])

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

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

:smile:

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