未找到应用程序。要么在视图函数中工作,要么推送应用程序上下文

我试图将 Flask-SQLAlchemy 模型分离到单独的文件中,当我试图运行 db.create_all()时,我得到 No application found. Either work inside a view function or push an application context.

返回文章页面

from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()

返回文章页面

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from shared.db import db


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

返回文章页面

from shared.db import db


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email_address = db.Column(db.String(300), unique=True, nullable=False)
password = db.Column(db.Text, nullable=False)
74141 次浏览

Use with app.app_context() to push an application context when creating the tables.

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


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