我使用了this文档作为指南。但是设置完
url = 'postgresql://{0}:{1}@{2}/{3}'.format(database_settings['user'], database_settings['password'], database_settings['host'], database_settings['dbname'])
app.config['SQLALCHEMY_DATABASE_URI'] = url
app.config['SESSION_TYPE'] = 'sqlalchemy'
我遇到一个错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "sessions" does not exist
LINE 2: FROM sessions
因此,我通过手工编码(psql)创建了表。 但这并没有任何改变。 我确定麻烦在于定义架构,但是我找不到任何使之起作用的方法。 (使用flask + sqlalchemy很难设置模式(不是为模型,而是为应用程序))。我试图用架构创建orm.session,但这没有帮助。 在这一刻,我仍然看到那麻烦。烧瓶会议成功 发送请求以检查数据库中的session_id,但找不到表会话...
完整错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "sessions" does not exist
LINE 2: FROM 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 = %(session_id_1)s \n LIMIT %(param_1)s'] [parameters: {'param_1': 1, 'session_id_1': 'session:2d3cfe3f-acd9-42e8-998d-aa7111704db3'}]
init_app.py:
from flask import Flask
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from dev.config import database_settings
class AppObjectsInit(object):
_instance = None
def __new__(cls):
if not isinstance(cls._instance, cls):
cls._instance = object.__new__(cls)
cls._instance.__init_app()
return cls._instance
def __init__(self):
pass
def __init_app(self):
self.__app = Flask(__name__)
url = 'postgresql://{0}:{1}@{2}/{3}'.format(database_settings['user'], database_settings['password'], database_settings['host'], database_settings['dbname'])
self.__app.config['SQLALCHEMY_DATABASE_URI'] = url
self.__app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.__db = SQLAlchemy(self.__app)
self.__app.config['SESSION_TYPE'] = 'sqlalchemy'
Session(self.__app)
我的应用程序可以与架构中的其他表正常工作。但是我为每个模型设置
__table_args__ = {'schema': 'my_schema'}