【问题标题】:Peewee FlaskDB not accepting keyword arguments (eg. max_connections, stale_timeout)Peewee FlaskDB 不接受关键字参数(例如 max_connections、stale_timeout)
【发布时间】:2021-10-27 08:40:45
【问题描述】:

我一直在开发的应用程序使用 Python 3.9、Flask 2.0.2、peewee 3.14.4 和 MySQL 8.0 运行良好,除非我为 FlaskDB 配置选项字典添加配置选项。我需要在部署应用程序时添加 MySQL 配置选项(包括 SSL)。

可行的 FlaskDB 配置选项

    DATABASE = {
        'name': DB_NAME,
        'engine': 'peewee.MySQLDatabase',
        'user': DB_USERNAME,
        'passwd': DB_PASSWORD,
        'host': DB_HOST,
        'port': DB_PORT}

FlaskDB 配置选项不起作用

    DATABASE = {
        'name': DB_NAME,
        'engine': 'peewee.MySQLDatabase',
        'user': DB_USERNAME,
        'passwd': DB_PASSWORD,
        'host': DB_HOST,
        'port': DB_PORT,
        'max_connections': 32,
        'stale_timeout': 300}

我从 uwsgi 日志收到的错误

Traceback (most recent call last):
  File "/app/./wsgi.py", line 4, in <module>
    from run import app
  File "/app/./run.py", line 6, in <module>
    app = create_app()
  File "/app/./ticketsapi/__init__.py", line 33, in create_app
    from ticketsapi.users.routes import users
  File "/app/./ticketsapi/users/routes.py", line 8, in <module>
    from ticketsapi.models import User, Post
  File "/app/./ticketsapi/models.py", line 302, in <module>
    initalisedB()
  File "/app/./ticketsapi/models.py", line 233, in initalisedB
    if User.table_exists():
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 6658, in table_exists
    return cls._schema.database.table_exists(M.table.__name__, M.schema)
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3310, in table_exists
    return table_name in self.get_tables(schema=schema)
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 4011, in get_tables
    return [table for table, in self.execute_sql(query, ('VIEW',))]
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3142, in execute_sql
    cursor = self.cursor(commit)
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3126, in cursor
    self.connect()
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3080, in connect
    self._state.set_connection(self._connect())
  File "/app/venv/lib/python3.9/site-packages/peewee.py", line 3982, in _connect
    conn = mysql.connect(db=self.database, **self.connect_params)
TypeError: __init__() got an unexpected keyword argument 'max_connections'

查看 peewee 文档,版本 3.14.4 的第 287 页,我的语法看起来是有效的。来自 peewee 文档:

    DATABASE = {
        'name': 'my_app_db',
        'engine': 'playhouse.pool.PooledPostgresqlDatabase',
        'user': 'postgres',
        'max_connections': 32,
        'stale_timeout': 600,
        }

对我哪里出错有什么想法吗?

【问题讨论】:

    标签: python python-3.x peewee


    【解决方案1】:

    显而易见的答案,我需要在配置选项中添加playhouse.pool.PooledMySQLDatabase

        DATABASE = {
            'name': DB_NAME,
            'engine': 'playhouse.pool.PooledMySQLDatabase',
            'user': DB_USERNAME,
            'passwd': DB_PASSWORD,
            'host': DB_HOST,
            'port': DB_PORT,
            'max_connections': 32,
            'stale_timeout': 300}
    

    【讨论】:

      猜你喜欢
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2021-09-24
      • 2018-11-22
      • 2018-10-26
      • 2012-07-27
      相关资源
      最近更新 更多