SQLAlchemy Flask 错误:The current Flask app is not registered with this 'SQLAlchemy' instance. Did you forget to call 'init_app', or did you create multiple 'SQLAlchemy' instances?

RuntimeError: The current Flask app is not registered with this 'SQLAlchemy' instance. Did you forget to call 'init_app', or did you create multiple 'SQLAlchemy' instances?

解决方法:
保证它只会创建一次(创建和使用分离)
创建一个extensions.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

application.py

...
from extensions import db

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = \
    'mysql://root:******@127.0.0.1/mysql'

db.init_app(app)
...

其它地方引入使用

...
from extensions import db
...
posted @ 2024-04-29 05:48  Excel2016  阅读(64)  评论(0编辑  收藏  举报