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

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.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

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.

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

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