flask 数据库迁移
时间: 2024-01-09 22:05:43 浏览: 122
Flask-Migrate是一个Flask扩展,它可以轻松地处理数据库迁移。下面是使用Flask-Migrate进行数据库迁移的步骤:
1.安装Flask-Migrate扩展
```shell
pip install Flask-Migrate
```
2.在Flask应用程序中初始化Flask-Migrate
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/db_name'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
```
3.创建迁移存储库
```shell
flask db init
```
4.创建迁移脚本
```shell
flask db migrate -m "initial migration"
```
5.应用迁移
```shell
flask db upgrade
```
6.如果需要回滚迁移,可以使用以下命令
```shell
flask db downgrade
```
阅读全文