flask 与 flask_migrate的使用
一、安装
pip install Flask-Migrate
二、简单使用
# 文件:manage.py
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
DBUser = "root"
DBPwd = "root"
DBName = "flask_db_1"
DBHost = "127.0.0.1"
DBPort = 3306
database_url = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(
DBUser, DBPwd, DBHost, DBPort, DBName)
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = database_url
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy()
migrate = Migrate()
migrate.init_app(app=app, db=db)
db.init_app(app)
class User(db.Model):
id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(32))
if __name__ == '__main__':
app.run()
三、通过终端进行数据库迁移操作
首先进入该文件的目录下
相应执行下面的命令
set FLASK_APP=manage.py
flask db init
flask db migrate
flask db upgrade
执行完毕之后,你再去数据库中查看,新建加了一些表,其中就是user表