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

Christopher Stuart
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Stuart
Full Stack JavaScript Techdegree Graduate 27,771 Points

Connection Already Open

Hi all, i'm working on the Flask Social Network project. I'm following along in Pycharm. It appears that anytime i go to the browser to check out a page (unless it is the first click of the instance) i'm getting a "peewee.OperationalError: Connection already open" error. Anyone else having this issue?

2 Answers

Christopher Stuart
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Stuart
Full Stack JavaScript Techdegree Graduate 27,771 Points

okay so this problem appears to be fixed for me (for now)

all i did was change DATABASE.connect() in models.py

to DATABASE.get_conn()

from Stack Overflow--they note that this ensures a connection is not opened twice. hope this can help someone else.

Changed to Answer

I tried to mimic what you were doing in your last post, with the tags() issue. I couldnt reproduce the .connect() issue even while using Database Transactions. My solution for you was going to be to change your .connect() to .get_conn() after a bunch of digging through Peewees GitHub repo.

I am also getting this issue:

peewee.OperationalError: Connection already open

It happens whenever I navigate to a resource which will cause an error and I don't have handlers (no 404 implemented). i.e. viewing a user that doesn't exist. The time when I go back to the index page, the the peewee error appears.

I tried to change the .connect() to .get_conn() but no luck.

Could you help? Thanks.

Anne Calija

Have you already defined something similar to this in your app?

# assumes you have imported g from flask module.
from flask import g

# ... along with all your other imports...


@app.before_request
def before_request():
    g.db = models.DATABASE # this assumes your models.py has a DATABASE variable set.
    g.db.connect() # connects to DB prior to each request.


@app.after_request
def after_request(response):
    g.db.close() # after request cycle finished, closes connection to DB.
    return response