flask-SQLAlchemy create_all()创建表失败报错

create_all()创建表失败报错:‘No application found. Either work inside a view function or push an application context.’

今天在用flask-SQLAlchemy时遇到的问题,总体而言我的问题和这位老兄一模一样:https://stackoverflow.com/questions/46540664/no-application-found-either-work-inside-a-view-function-or-push-an-application

解决方案

上下文设置问题,链接中大佬给出了解决方案:

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'My connection string'

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

我是db.create_all()时遇到的报错,也是一样加入到上下文中去

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

解决了,又走过了一个弯路,成就+1

你可能感兴趣的:(python,flask)