Re: [flask] init_app was not enough for flask-sqlalchemy
- From:
- Alex Morega
- Date:
- 2011-10-01 @ 07:11
On Oct 1, 2011, at 6:10 AM, Italo Maia wrote:
> Folks, I had a very weird problem with my tests in flask.
>
>
> As flask-script Manager class requires a Flask instance to be
instanciated, I was creating a simple Flask instance with Dev
configuration, then, in my tests create_app, I was creating the Flask
instance with Test configuration. Test and Dev configurations used
different databases. I noticed that, when calling db.create_all, if I had
not set db.app = (new flask instance with test config) before
db.init_app(app), my test database would never be created.
>
> Does that make sense to you guys? I have a feeling that db.init_db
should be enough. Anyway, this fix, db.app = app, i saw looking around
flask code.
I think I had the same problem, and the solution was to call db.create_all
in the context of the application. This code is run in the application
factory, so it creates tables for either test or dev (or production)
database.
with app.test_request_context():
db.create_all()
Cheers,
-- Alex