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

Yeeka Yau
Yeeka Yau
7,410 Points

TypeError: 'Staff' object is not subscriptable

Hi I am working through this tutorial using postgresql on my local machine and I tried to do as Kenneth suggested in the video, to check whether the points on a record is actually different before updating the DB - but I get this error:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\peewee.py", line 3191, in execute_sql
    cursor.execute(sql, params or ())
psycopg2.IntegrityError: duplicate key value violates unique constraint "staff_username"
DETAIL:  Key (username)=(yeyau) already exists.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\yeyau\Documents\tsc_exchange\staff.py", line 26, in add_staff
    Staff.create(username=s_member['username'],points=s_member['points']) #Staff is our db, create adds a new record to the db
  File "C:\Python34\lib\site-packages\peewee.py", line 4127, in create
    inst.save(force_insert=True)
  File "C:\Python34\lib\site-packages\peewee.py", line 4287, in save
    pk_from_cursor = self.insert(**field_dict).execute()
  File "C:\Python34\lib\site-packages\peewee.py", line 2980, in execute
    cursor = self._execute()
  File "C:\Python34\lib\site-packages\peewee.py", line 2470, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "C:\Python34\lib\site-packages\peewee.py", line 3199, in execute_sql
    self.commit()
  File "C:\Python34\lib\site-packages\peewee.py", line 3048, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "C:\Python34\lib\site-packages\peewee.py", line 123, in reraise
    raise value.with_traceback(tb)
  File "C:\Python34\lib\site-packages\peewee.py", line 3191, in execute_sql
    cursor.execute(sql, params or ())
peewee.IntegrityError: duplicate key value violates unique constraint "staff_username"
DETAIL:  Key (username)=(yeyau) already exists.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\yeyau\Documents\tsc_exchange\staff.py", line 45, in <module>
    add_staff()
  File "C:\Users\yeyau\Documents\tsc_exchange\staff.py", line 31, in add_staff
    if s_member_record['points'] != s_member['points']:
TypeError: 'Staff' object is not subscriptable

My code is:

def add_staff():
    for s_member in staff_list:
        try:
            Staff.create(username=s_member['username'],points=s_member['points']) 
        except IntegrityError:
            db.rollback()
            s_member_record = Staff.get(username=s_member['username'])

            if s_member_record['points'] != s_member['points']:
                s_member_record.points = s_member['points']
                s_member_record.save()

It seems to be having a problem with my if statement, because when I remove that line - I don't get any errors and the whole thing works fine.

Some similar questions on Stackoverflow seems to suggest some ambiguity with the naming can cause a problem, so I changed the list to be staff_list. Still no luck though...

Any help would be greatly appreciated!

1 Answer

Frederick Pearce
Frederick Pearce
10,677 Points

I think the s_member_record['points'] in your if statement is giving the trouble. Change to s_member_record.points as shown below:

def add_staff():
    for s_member in staff_list:
        try:
            Staff.create(username=s_member['username'],points=s_member['points']) 
        except IntegrityError:
            db.rollback()
            s_member_record = Staff.get(username=s_member['username'])

            if s_member_record.points != s_member['points']:
                s_member_record.points = s_member['points']
                s_member_record.save()

I checked this and didn't get an error. Hope that helps!

Yeeka Yau
Yeeka Yau
7,410 Points

That's great. Thanks for your help - worked perfectly! I think I understand the issue. The object type of s_member_record, is not a dictionary, but an object, so i was referring to it incorrectly.