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 trialChristopher Stuart
Full Stack JavaScript Techdegree Graduate 27,771 PointsConnection 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
Full Stack JavaScript Techdegree Graduate 27,771 Pointsokay 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.
Christopher Stuart
Full Stack JavaScript Techdegree Graduate 27,771 Pointsah...thanks for looking into this for me Chris!
annecalija
9,031 PointsI 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.
Chris Howell
Python Web Development Techdegree Graduate 49,610 PointsHave 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
Chris Howell
Python Web Development Techdegree Graduate 49,610 PointsChris Howell
Python Web Development Techdegree Graduate 49,610 PointsChanged 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.