sqlite3.OperationalError) 没有这样的 table - 带有 huey 任务队列的烧瓶

sqlite3.OperationalError) no such table - flask with huey task queue

我正在尝试使用 python huey (https://github.com/coleifer/huey/blob/master/huey/api.py) 来允许使用带有 Flask 的任务队列。

我的 app/init 包含:

from flask_sqlalchemy import SQLAlchemy

mail = Mail()
db = SQLAlchemy()



def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # not using sqlalchemy event system, hence disabling it

    config[config_name].init_app(app)

    # Set up tasking
    mail.init_app(app)
    db.init_app(app)
    ......

    return app


def create_huey_app():
    _app = Flask('huey-app')
    db.init_app(_app)

    from app.models.other_models import Postings, Accounts

    return _app

我的 huey 任务文件夹包含:

@my_huey.task()
def create_listing_task(messages):
    """Background task to send an email with Flask-Mail."""
    h_app = create_huey_app()
    with h_app.app_context():
        create_listing(messages)

在我的烧瓶视图中我有:

@main.route('/selected')
def selected():

....

    res  = create_listing_task(messages)
    res(blocking=True, timeout=5)  # Block for up to 5 seconds

    return 'fin'

我得到的错误是

Traceback (most recent call last):
  File "...\lib\site-packages\flask\app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "...\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "...\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "...\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "...\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "...\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "...\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "...\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "...\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "...\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "...\app\main\views.py", line 99, in selected
    res(blocking=True, timeout=100)  # Block for up to 5 seconds
  File "...\lib\site-packages\huey\api.py", line 701, in __call__
    return self.get(*args, **kwargs)
  File "...\lib\site-packages\huey\api.py", line 743, in get
    raise TaskException(result.metadata)
huey.exceptions.TaskException: OperationalError('(sqlite3.OperationalError) no such table: postings',)

虽然我正在初始化 flask-sqlalchemy 扩展程序,但应用程序似乎没有找到这些表。我怎样才能让它工作?

表格不会自动创建。您需要执行db.create_all()。参见 Flask-SQLAlchemy quickstart.