librelist archives

« back to archive

Problem with gunicorn+flask+sqlalchemy

Problem with gunicorn+flask+sqlalchemy

From:
Eduardo Robles Elvira
Date:
2013-07-24 @ 17:31
Hello,

I'm having trouble with using sqlalchemy in flask when using gunicorn.
It works fine without it, but when using gunicorn somehow it stops
working. I've created a simple test that will allow you to easily see
the problem, see here: https://gist.github.com/edulix/6069993

The example uses sqlite, but the same happens when using postgresql.
This is the error:

OperationalError: (OperationalError) no such table: foo u'INSERT INTO
foo (name) VALUES (?)' (u'bar',)

And as you can see and test in the example, the table clearly exists.

¿Any ideas?

Thanks in advance,
--
Eduardo Robles Elvira     +34 668 824 393            skype: edulix2
http://www.wadobo.com    it's not magic, it's wadobo!

Re: [flask] Problem with gunicorn+flask+sqlalchemy

From:
Robson Roberto Souza Peixoto
Date:
2013-07-24 @ 21:59
If you config it before, your code will work.
https://gist.github.com/robsonpeixoto/6074949

But I don't known why!



On Wed, Jul 24, 2013 at 2:31 PM, Eduardo Robles Elvira <edulix@wadobo.com>wrote:

> Hello,
>
> I'm having trouble with using sqlalchemy in flask when using gunicorn.
> It works fine without it, but when using gunicorn somehow it stops
> working. I've created a simple test that will allow you to easily see
> the problem, see here: https://gist.github.com/edulix/6069993
>
> The example uses sqlite, but the same happens when using postgresql.
> This is the error:
>
> OperationalError: (OperationalError) no such table: foo u'INSERT INTO
> foo (name) VALUES (?)' (u'bar',)
>
> And as you can see and test in the example, the table clearly exists.
>
> ¿Any ideas?
>
> Thanks in advance,
> --
> Eduardo Robles Elvira     +34 668 824 393            skype: edulix2
> http://www.wadobo.com    it's not magic, it's wadobo!
>



-- 
Robson Roberto Souza Peixoto
Robinho http://robsonpeixoto.com/
Master in Computer Science, University of Campinas
Linux Counter #395633
IRC: robsonpeixoto
Twitter: http://twitter.com/rrspba
github: https://github.com/robsonpeixoto

Re: [flask] Problem with gunicorn+flask+sqlalchemy

From:
Eduardo Robles Elvira
Date:
2013-07-24 @ 22:59
On Wed, Jul 24, 2013 at 11:59 PM, Robson Roberto Souza Peixoto
<robsonpeixoto@gmail.com> wrote:
> If you config it before, your code will work.
> https://gist.github.com/robsonpeixoto/6074949
>
> But I don't known why!

Hi:

So the config needs to be loaded before db is created? that's
strange.. Looks like some kind of bug to me. It's also weird that
app.config.from_object(__name__) is called before the
SQLALCHEMY_DATABASE_URI is set (but then called again)

Regards,
-- 
Eduardo Robles Elvira     +34 668 824 393            skype: edulix2
http://www.wadobo.com    it's not magic, it's wadobo!

Re: [flask] Problem with gunicorn+flask+sqlalchemy

From:
Nguyễn Hồng Quân
Date:
2013-07-25 @ 09:12
2013/7/25 Eduardo Robles Elvira <edulix@wadobo.com>

>
> So the config needs to be loaded before db is created? that's
> strange.. Looks like some kind of bug to me. It's also weird that
> app.config.from_object(__name__) is called before the
> SQLALCHEMY_DATABASE_URI is set (but then called again)
>
>
I think that because when line 10
db = SQLAlchemy(app)
is executed, "app" still doesn't have SQLALCHEMY_DATABASE_URI and hence,
"db" doesn't know where is database file.
Then, when you run at line 38:
app.config.from_object(__name__)
only "app" is updated with SQLALCHEMY_DATABASE_URI, not "db", "db" still
doesn't know where data file is.

-- 
***********************************************
* Nguyễn Hồng Quân                            *
* Y!M: ng_hquan_vn                            *
* Identi.ca: hongquan                         *
***********************************************

Re: [flask] Problem with gunicorn+flask+sqlalchemy

From:
Eduardo Robles Elvira
Date:
2013-07-25 @ 20:15
On Thu, Jul 25, 2013 at 11:12 AM, Nguyễn Hồng Quân
<ng.hong.quan@gmail.com> wrote:
> I think that because when line 10
> db = SQLAlchemy(app)
> is executed, "app" still doesn't have SQLALCHEMY_DATABASE_URI and hence,
> "db" doesn't know where is database file.
> Then, when you run at line 38:
> app.config.fro m_object(__name__)
> only "app" is updated with SQLALCHEMY_DATABASE_URI, not "db", "db" still
> doesn't know where data file is.


Hello people:

Thanks for the help! I finally found the problem: it was that the code
after 'if __name__ == "__main__":' is not executed when using
gunicorn, that's why putting the app.config.from_object(__name__) line
before that solves the problem =)

Regards,
-- 
Eduardo Robles Elvira     +34 668 824 393            skype: edulix2
http://www.wadobo.com    it's not magic, it's wadobo!