我需要创建一个会话表来使用Flask-Session SqlAlchemySessionInterface [英] Do I need to create a sessions table to use Flask-Session SqlAlchemySessionInterface

查看:184
本文介绍了我需要创建一个会话表来使用Flask-Session SqlAlchemySessionInterface的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的python应用程序中实现Flask-Session.我在文档中读到,建议使用其他接口,例如SqlAlchemySessionInterface,而不是默认的NullSessionInterface,该接口在SESSION_TYPE配置键未提供任何内容时使用.

I am attempting to implement Flask-Session in my python application. I read in the docs that its recommended to use another interface like the SqlAlchemySessionInterface instead of the default NullSessionInterface which is used when nothing is provided to the SESSION_TYPE configuration key.

从Session类下的flask_session/ init .py文件读取

From the flask_session/init.py file under class Session it reads

默认情况下,Flask-Session将使用:class:NullSessionInterface 确实应该将您的应用程序配置为使用其他SessionInterface.

By default Flask-Session will use :class:NullSessionInterface, you really should configurate your app to use a different SessionInterface.

SESSION_TYPE配置键设置为"sqlalchemy"后,出现错误

After setting the SESSION_TYPE configuration key to "sqlalchemy" I get an error

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "sessions" does not exist

这表明Flask-Session正在数据库模型中使用名称为"sessions"的表,但是我在Flask-Session文档中找不到任何地方指出该表应该创建以及该表包含哪些字段应该有.

This indicates that Flask-Session is looking to use a table with the name "sessions" in my database model but I cannot find anywhere in the Flask-Session documentation where it points out that a table should be created and what fields it should have.

有人可以提出解决方案吗?

Can anyone suggest a solution to this please?

推荐答案

我想使用Flask-session,但是我也在使用Flask-migrate,并且不想手动调用db.create_all()来中断迁移.小路.幸运的是,@ Flashspeedlife的建议只是导入接口并实例化它即可.

I wanted to use Flask-session, but I was also using Flask-migrate and didn't want to call db.create_all() manually and break the migration path. Fortunately, @Flashspeedlife's suggestion of just importing the Interface and instantiating it worked.

app/__ init __.py:

from flask_session import SqlAlchemySessionInterface
from app.extensions import db, sess, migrate # My extensions file

def create_app():
    app = Flask(__name__)
    with app.app_context():
         db.init_app(app)
         migrate.init_app(app, db)

         sess.init_app(app)
         SqlAlchemySessionInterface(app, db, "sessions", "sess_")

现在,flask db migrate使用新的会话表生成一个Alembic脚本.

Now, flask db migrate generates an alembic script with the new sessions table.

这篇关于我需要创建一个会话表来使用Flask-Session SqlAlchemySessionInterface的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆