Flask-SQLAlchemy:SQLALCHEMY_ENGINE_OPTIONS设置不正确 [英] Flask-SQLAlchemy: SQLALCHEMY_ENGINE_OPTIONS not set up correctly
问题描述
我刚刚将项目Flask-SQLAlchemy版本更新为最新版本(v2.4).由于不建议使用某些SQL-Alchemy配置参数,因此我现在按照文档进行操作,并将SQLALCHEMY_ENGINE_OPTIONS作为字典添加到我的配置类中.但是,当我尝试查询数据库时,出现错误.
I just updated my projects Flask-SQLAlchemy Version to the latest one (v2.4). As some of the SQL-Alchemy config parameters were deprecated, I now follow the documentation and added SQLALCHEMY_ENGINE_OPTIONS as a dictionary to my config class. However, when I try to query the database I get an error.
我正在查找sqlalchemy的create_engine()所需的确切关键字.
I was looking up the exact keywords needed for sqlalchemy's create_engine().
这是我的配置类:
class ConfigAPI:
try:
SQLALCHEMY_DATABASE_URI = os.environ['MYSQL_URI']
except KeyError as e:
logging.warning('FAILED DEFINING MYSQL PARAMETER')
logging.fatal(e)
sys.exit(1)
SECRET_KEY = '123456asdsadfsdfasadfa67893nvkabl790'
SQLALCHEMY_ECHO = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENGINE_OPTIONS = {
'pool': QueuePool,
'pool_size' : 10,
'pool_recycle':120,
'pool_pre_ping': True
}
这是我的app_factory文件
And here is my app_factory file
db = SQLAlchemy()
def create_api(config=ConfigAPI):
app = Flask(__name__)
from app_projects.internal_api.api_v0 import blueprint as v0
app.config.from_object(config)
db.init_app(app)
cors.init_app(app)
app.register_blueprint(v0)
return app
这是我收到的错误:
TypeError: Invalid argument(s) 'pool_size','pool_recycle','pool_pre_ping' sent to create_engine(), using configuration MySQLDialect_pymysql/type/Engine. Please check that the keyword arguments are appropriate for this combination of components.
我在这里想念什么?
推荐答案
正如您已经在注释中提到的那样,问题出在pool参数中.
As you have already mentioned in the comment the problem was in the pool parameter.
SQLAlchemy文档指定Pool
或QueuePool
的实例可能是pool
参数.
SQLAlchemy documentation specifies that an instance of Pool
or QueuePool
could be a pool
parameter.
有效的示例是:
SQLALCHEMY_ENGINE_OPTIONS = {
'pool': QueuePool(creator),
'pool_size': 10,
'pool_recycle': 120,
'pool_pre_ping': True
}
这篇关于Flask-SQLAlchemy:SQLALCHEMY_ENGINE_OPTIONS设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!