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

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Database is locked even after deleting social.db and restarting app.py

Hello guys.

It seems like I have something wrong in my code again.

I got that database is locked peewee message so as suggested I deleted social.db (our database).

The fact is that if I restart the app it works and I get to the registration form, but if I register a random username I get that database is locked error message again.

I have been looking at docs and googling and I understood that it means that there must be a process that is using the database so we can't access it at the moment.

I have reviewed the code and the only process (I think) that may do this is in app.py when we do this:

try:
        models.User.create_user(
            username='Vittorio',
            email='vitovito22@gmail.com',
            password='password',
            admin=True
        )
    except ValueError:
        pass

I wanted to stick with this idea that I had and I literally removed that whole portion of code to try if my registration form would work, and after deleting social.db once again, I am able to register a new user and it seems that it all works ok.

At this point I have checked my code again and again against the video, but can really see any difference, so I was wondering:

Does creating a new user work for everyone? Has someone actually tried to submit the full form? Because in the following videos Kenneth only tries to login with the user that is created by those lines of code above here and I do not see him creating a "different" user in fact.

I have also been thinking that I may have forgotten to close the connection somewhere but honestly I have no idea where as I think this is the only place where the error could be.

Obviously I may be wrong :D

Any ideas?

Ty

Vittorio

7 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

OK, so, a bit of digging and, no, I can't create one at that point. I checked some later Workspaces and this is the new create_user method:

@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")

Notice that we're doing all of the work in a transaction. This seems to stop the weird locked database thing. I'll add it to Teacher's Notes and update the Workspace at that point.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Great!

That works just fine!!

Thanks for the help Kenneth, much appreciated

Vittorio

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I remember being plagued with these, off and on, during filming. They would go away when I killed the server or deleted the database most of the time (as mentioned during the course). Have you compared your code above with what's in either a later Workspace (you'll have to close yours and open a new one next to a video) or in the project downloads?

I don't see anything in your code that throws up a red flag for me.

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Nice hint!

Actually I have tried this: I have launched the "Add Some Layout" workspace (with my previous processes killed and previous workspace closed); then I removed social.db and launched the app.

I have tried to create a new user and I still get the database locked message.

Just to make sure Kenneth , does it work for you? I mean: after creating the macro, at that point, have you been able to create a new user? Excluding the one we create directly with our code that has username='kennethlove' in the video (this does work for me as well)...

Even after creating the login and logout view, as I do not think it makes any difference for this.

I think we might need to close the connection after that coded-created user or something similar as I do think that is the process that keeps the database busy. This may lead to adding something in the create_user method I would imagine.

Also I know I am super new to this and I hope I do not sound arrogant but I could not check this particular thing against the video as I can only see you testing the validators but I don't see the full registration happening in the video.

Kindly let me know, I am pretty curious at this point.

Thanks very much.

Vittorio

Yeah, I found myself opening a Python console and doing User.drop_tables([User, Post, Relationship]) to delete the tables, then restart the app which when initiated would create the new tables.

I have been cruising in this course, and finally feeling traction after years of trying unsuccessfully to learn to program. I got stuck with this locked database issue.

I switched to the code that Kenneth updated. After some indentation errors, I am now getting an error that says

<code>Traceback (most recent call last):
File "app.py", line 62, in <module>
models.User.create_user(
AttributeError: type object 'User' has no attribute 'create_user' </code>

I am stuck, and I don't understand. Please help me.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

There should be a def create_user inside of your User class in models.py. Is that there?

I am assuming your indentation error moved the create_user out of the the User class. Remember not to have create_user all the way to column 1. It needs to be indented to be in the User class.

I had a 'peewee.OperationalError: unable to open database file' error using Pycharm. It appeared to be a Pycharm issue or smth. Running script from the command line (i'm using windows) works well.