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

本文解决了一个在使用Flask-SQLAlchemy时遇到的常见问题:create_all()创建表失败,报错‘No application found’。通过正确设置应用上下文,成功解决了这一问题。

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

(venv) gapinyc@DESKTOP-9QS7RL5:~/superset$ pip install --upgrade pymysql sqlalchemy flask-sqlalchemy Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Requirement already satisfied: pymysql in ./venv/lib/python3.10/site-packages (1.1.2) Requirement already satisfied: sqlalchemy in ./venv/lib/python3.10/site-packages (1.4.54) Collecting sqlalchemy Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4a/d8/c63d8adb6a7edaf8dcb6f75a2b1e9f8577960a1e489606859c4d73e7d32b/sqlalchemy-2.0.44-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2/3.2 MB 11.5 MB/s 0:00:00 Requirement already satisfied: flask-sqlalchemy in ./venv/lib/python3.10/site-packages (2.5.1) Collecting flask-sqlalchemy Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1d/6a/89963a5c6ecf166e8be29e0d1bf6806051ee8fe6c82e232842e3aeac9204/flask_sqlalchemy-3.1.1-py3-none-any.whl (25 kB) Requirement already satisfied: greenlet>=1 in ./venv/lib/python3.10/site-packages (from sqlalchemy) (3.1.1) Requirement already satisfied: typing-extensions>=4.6.0 in ./venv/lib/python3.10/site-packages (from sqlalchemy) (4.15.0) Requirement already satisfied: flask>=2.2.5 in ./venv/lib/python3.10/site-packages (from flask-sqlalchemy) (2.3.3) Requirement already satisfied: Werkzeug>=2.3.7 in ./venv/lib/python3.10/site-packages (from flask>=2.2.5->flask-sqlalchemy) (3.1.3) Requirement already satisfied: Jinja2>=3.1.2 in ./venv/lib/python3.10/site-packages (from flask>=2.2.5->flask-sqlalchemy) (3.1.6) Requirement already satisfied: itsdangerous>=2.1.2 in ./venv/lib/python3.10/site-packages (from flask>=2.2.5->flask-sqlalchemy) (2.2.0) Requirement already satisfied: click>=8.1.3 in ./venv/lib/python3.10/site-packages (from flask>=2.2.5->flask-sqlalchemy) (8.3.0) Requirement already satisfied: blinker>=1.6.2 in ./venv/lib/python3.10/site-packages (from flask>=2.2.5->flask-sqlalchemy) (1.9.0) Requirement already satisfied: MarkupSafe>=2.0 in ./venv/lib/python3.10/site-packages (from Jinja2>=3.1.2->flask>=2.2.5->flask-sqlalchemy) (3.0.3) Installing collected packages: sqlalchemy, flask-sqlalchemy Attempting uninstall: sqlalchemy Found existing installation: SQLAlchemy 1.4.54 Uninstalling SQLAlchemy-1.4.54: Successfully uninstalled SQLAlchemy-1.4.54 Attempting uninstall: flask-sqlalchemy Found existing installation: Flask-SQLAlchemy 2.5.1 Uninstalling Flask-SQLAlchemy-2.5.1: Successfully uninstalled Flask-SQLAlchemy-2.5.1 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. flask-appbuilder 4.8.1 requires Flask-SQLAlchemy<3,>=2.4, but you have flask-sqlalchemy 3.1.1 which is incompatible. flask-appbuilder 4.8.1 requires SQLAlchemy<1.5, but you have sqlalchemy 2.0.44 which is incompatible. apache-superset 5.0.0 requires sqlalchemy<2,>=1.4, but you have sqlalchemy 2.0.44 which is incompatible. Successfully installed flask-sqlalchemy-3.1.1 sqlalchemy-2.0.44 (venv) gapinyc@DESKTOP-9QS7RL5:~/superset$
最新发布
10-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值