5

I'm trying to enable server-side sessions for my flask application using a SQLite database. Note I'm using Flask-Sessionstore which is a hard fork of Flask-Session and is more actively maintained. However, I'm getting the same error when I use both libraries. Here is my code for app.py:

from flask import Flask, session
from flask_sqlalchemy import SQLAlchemy
from flask_sessionstore import Session

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////Users/Johnny/DBs/test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # quiet warning message
app.config['SESSION_TYPE'] = 'sqlalchemy'
Session(app)

@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(debug=True)

When I run the application with python app.py everything starts and runs as expected. However, when I try to access the index page at http://127.0.0.1:5000 I get the following error:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: sessions [SQL: 'SELECT sessions.id AS sessions_id, sessions.session_id AS sessions_session_id, sessions.data AS sessions_data, sessions.expiry AS sessions_expiry \nFROM sessions \nWHERE sessions.session_id = ?\n LIMIT ? OFFSET ?'] [parameters: ('session:xxx...', 1, 0)]

It appears I need to create this table before running the application. How can I do this? Or am I missing a configuration option that creates the table for me if it's not found?

Johnny Metz
  • 5,977
  • 18
  • 82
  • 146
  • 1
    In case you stumble on this question in 2019: The linked github site of Flask-Session says it is not longer maintained. – otwtm Jun 12 '19 at 13:30
  • 1
    Update for July 2020. https://flasksession.readthedocs.io/en/latest/ seems to be current again and this github repo was recently updated https://github.com/fengsp/flask-session. Hope this helps. – Isd Sava Jul 16 '20 at 06:41

3 Answers3

9

You will need to provide this in your app config as well:

app.config[SESSION_SQLALCHEMY_TABLE] = 'sessions'
app.config[SESSION_SQLALCHEMY] = db

where db is sqlAlchemy instance. So you should have something like:

db = SQLAlchemy(app)
session = Session(app)
session.app.session_interface.db.create_all()

You can take a look at the test example of flask_sessionstore on github

Mekicha
  • 811
  • 9
  • 21
  • 4
    I don't need the two config lines because they are already the defaults. However, that last line `session.app.session_interface.db.create_all()` is exactly what I was looking for thanks! – Johnny Metz Aug 25 '17 at 19:47
1

The answer from @Mekicha works but if you already have your database and don't want to run db.create_all(), consider this alternative:

After you initialize your session instance with the app, you can use the sqlalchemy interface to build your 'session' table, if none is existent:

SqlAlchemySessionInterface(app, db, "sessions", "sess_")

Then run a migration against your database.

See this related post for a full example

kip2
  • 6,473
  • 4
  • 55
  • 72
  • yes, if you are using alembic directly to play migrations. Adding this line to alembic/env.py permit to register Session table in db.metadata before auto-generating migrations – kheraud Apr 22 '21 at 13:40
0

As of 2022, you should use the following:

# assuming you have already have a Flask instance, an SQLAlchemy instance, and a Session instance:
app.session_interface.sql_session_model.__table__.create(bind = db.session.bind)
Maytha8
  • 846
  • 1
  • 8
  • 26
  • Where does this come from? – anonymous Aug 26 '22 at 13:25
  • @anonymous `app.session_interface.sql_session_model` is set by `flask-session` [in the source code](https://github.com/fengsp/flask-session/blob/9f591b5c7ecf25c44678c7d18d94a0bf91132e2d/flask_session/sessions.py#L514), and `Model.__table__.create` is referenced [in this SO question](https://stackoverflow.com/a/34240889) ([docs](https://docs.sqlalchemy.org/en/14/core/metadata.html#sqlalchemy.schema.Table.create)). Was also tested on my own Flask project. – Maytha8 Aug 27 '22 at 06:02