flask-session icon indicating copy to clipboard operation
flask-session copied to clipboard

SQLAlchemy db.create_all() causing errors

Open rsyring opened this issue 10 years ago • 1 comments

The use of self.db.create_all() in SqlAlchemySessionInterface is causing me some problems:

https://github.com/fengsp/flask-session/blob/master/flask_session/sessions.py#L458

When using two-step invocation, there is no guarantee of an app context available when session.init_app() is run. The call to db.create_all() results in an error like:

RuntimeError: application not registered on db instance and no application bound to current context

Furthermore, I don't think it's the job of Flask-Session to call this method anyway. If the table needs to be created, IMO that needs to be handled by the application developer.

Suggestions:

  1. Just remove the call to db.create_all(); or
  2. Add a configuration option that would allow us to skip this method being called.

If you decide on option 2 above, then IMO the call to db.create_all() should still be replaced by code that only creates the session table. Based on these docs:

http://docs.sqlalchemy.org/en/latest/core/metadata.html

That would look something like the following for SqlAlchemySessionInterface.__init__():

class Session(self.db.model):
...

if create_session_table:
    Session.table.create(db.engine)

I'd be happy to create a PR if you are interested.

rsyring avatar Jun 11 '15 12:06 rsyring

This issue was moved to mcrowson/flask-session#9

mcrowson avatar Feb 12 '17 00:02 mcrowson

Fixed since 0.6.0

Lxstr avatar Feb 25 '24 13:02 Lxstr