我和这个线程有同样的问题:Reflecting tables with Flask-SQLAlchemy raises RuntimeError: application not registered 我试着添加:
db.reflect(app=app)
在数据库初始化方法中,但我仍然得到:
File "C:\Users\myuser\PycharmProjects\server\app\models\__init__.py", line 11, in Entry
__table__ = db.Table('entry', db.metadata, autoload=True, autoload_with=db.engine)
File "C:\Users\myuser\PycharmProjects\server\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 998, in engine
return self.get_engine()
File "C:\Users\myuser\PycharmProjects\server\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1007, in get_engine
app = self.get_app(app)
File "C:\Users\myuser\PycharmProjects\server\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1042, in get_app
raise RuntimeError(
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
使用 AppFactory 结构时如何在没有这些问题的情况下反映表?
楷模:
from app.extensions import db
class Entry(db.Model):
__table__ = db.Table('entry', db.metadata, autoload=True, autoload_with=db.engine)
class Room(db.Model):
__table__ = db.Table('room', db.metadata, autoload=True, autoload_with=db.engine)
应用程序:
from os import getenv
from flask import Flask, make_response, jsonify
from flask_cors import CORS
from app.extensions import db, migrate, socketio
def create_app(blueprints=None) -> Flask:
"""
Builds up a Flask app and return it to the caller
:param blueprints: a custom list of Flask blueprints
:return: Flask app object
"""
if blueprints is None:
blueprints = DEFAULT_BLUEPRINTS
app = Flask(__name__)
configure_app(app)
configure_extensions(app)
configure_error_handlers(app)
configure_blueprints(app, blueprints)
print("Returning app...")
return app
def configure_app(app):
app.config.from_object(getenv('APP_SETTINGS', "config.DevelopmentConfig"))
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
def configure_blueprints(app, blueprints):
for blueprint in blueprints:
app.register_blueprint(blueprint)
def configure_extensions(app):
db.init_app(app)
migrate.init_app(app, db, directory="migrations")
socketio.init_app(app, cors_allowed_origins='*')
CORS(app, resources={r"*": {"origins": "*"}})
def configure_error_handlers(app):
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
# Return validation errors as JSON
@app.errorhandler(422)
@app.errorhandler(400)
def handle_error(err):
headers = err.data.get("headers", None)
messages = err.data.get("messages", ["Invalid request."])
if headers:
return jsonify({"errors": messages}), err.code, headers
else:
return jsonify({"errors": messages}), err.code
我花了很多时间寻找这个问题的解决方案,但到目前为止我还没有运气。提前致谢!