使用Python+SQLAlchemy链接数据库并创建表时报错:

问题

使用Python+SQLAlchemy链接数据库并创建表时报错:

报错信息

RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.

环境

pymysql+ORM

with app.app_context():
    db.create_all()

这样写试试