【问题标题】:'No application found. Either work inside a view function or push' RuntimeError: No application found'没有找到应用程序。在视图函数中工作或推送'RuntimeError:未找到应用程序
【发布时间】:2020-03-29 18:30:31
【问题描述】:

在声称这是重复之前,我已经搜索了无数错误,但我无法找到适合我的解决方案。

我有一个烧瓶网络应用程序,我是 sql alchemy,以方便与我的 google sql 数据库的连接。在使用 db.create_all() 函数创建我的应用程序时,我需要创建与我的应用程序对应的表。但是,每次我这样做时都会收到错误消息:“'未找到应用程序。要么在视图函数中工作,要么推送” RuntimeError:未找到应用程序。在视图函数中工作或推送应用程序上下文"

这是我目前拥有的代码。如果您能提供任何帮助,我将不胜感激:

db = SQLAlchemy()

def create_app():
  app = Flask(__name__)

  app.config.from_object(config)

  db.init_app(app)

  login_manager = LoginManager()
  login_manager.login_view = 'auth.login'
  login_manager.init_app(app)

  from .models import User

  @login_manager.user_loader
  def load_user(user_id):
    return User.query.get(int(user_id))

  from .auth import auth as auth_blueprint
  app.register_blueprint(auth_blueprint)

  from .main import main as main_blueprint
  app.register_blueprint(main_blueprint)

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

  return app

【问题讨论】:

    标签: flask sqlalchemy flask-sqlalchemy google-cloud-sql


    【解决方案1】:

    尝试替换

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

    app.app_context().push()
    db.create_all()
    

    更多信息请参阅flask_sqlalchemy contexts docs

    顺便说一句,您正在使用一个新创建的SQLAlchemy 类,它不知道必须将哪个结构应用于新数据库。您应该使用由您的模型创建的 db 对象from .models import User, db

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2018-06-09
      • 2019-02-15
      相关资源
      最近更新 更多