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 Build a Social Network with Flask Takin' Names Macros

Database locked + some more errors

So I am getting a database locked same as what Kenneth got and have used the solution in the Teachers comment, but still same thing. I am using Pycharm and tried to used Workspaces too but I can't be able to see a database being created so I could delete it and try again. Here is the error I get when I try to run it.

EDIT: GOT IT TO WORK IN WORKSPACES, if anyone knows why the db doesn't show up in Pycharm please let me know. I tried restarting Pycharm and still it doesn't show up.

During handling of the above exception, another exception occurred:                               

Traceback (most recent call last):                                                                
  File "SocialNetwork.py", line 70, in <module>                                                   
    admin=True                                                                                    
  File "/home/treehouse/workspace/models.py", line 30, in create_user                             
    is_admin=admin)                                                                               
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 3587, in crea
te                                                                                                
    inst.save(force_insert=True)                                                                  
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 3719, in save
    pk_from_cursor = self.insert(**field_dict).execute()                                          
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 2610, in exec
ute                                                                                               
    return self.database.last_insert_id(self._execute(), self.model_class)                        
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 2172, in _exe
cute                                                                                              
    return self.database.execute_sql(sql, params, self.require_commit)                            
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 2788, in exec
ute_sql                                                                                           
    self.commit()                                                                                 
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 2657, in __ex
it__                                                                                              
    reraise(new_type, new_type(*exc_value.args), traceback)                                       
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 115, in rerai
se                                                                                                
    raise value.with_traceback(tb)                                                                
  File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/peewee.py", line 2780, in exec
ute_sql                                                                                           
    cursor.execute(sql, params or ())                                                             
peewee.OperationalError: database is locked

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Some locked database issues are being solved using the solution locked database - Build a Social Network with Flask. The improved create_user method adds with DATABASE.transaction():

If you're constantly getting a locked database, change your User.create_user method to the following:

    @classmethod
    def create_user(cls, username, email, password, admin=False):
        try:
            with DATABASE.transaction():
                cls.create(
                    username=username,
                    email=email,
                    password=generate_password_hash(password),
                    is_admin=admin)
        except IntegrityError:
            raise ValueError("User already exists")
V K
V K
5,237 Points

Just want to say thanks for posting this. I was able to get my program to run properly after adding the DATABASE.transaction(): line.

Did you try to add the db in the module "Database" of PyCharm ? Maybe there's a missing driver that this module will propose you to install ...

how would I do that?

David Axelrod
David Axelrod
36,073 Points

I'm getting the exact same error

Are you using Pycharm?