Flask-Migrate的介绍、安装、使用 1、介绍:因为采用'db.create_all'在后期修改数据库表字段的时候,不会自动的映射到数据库中,必须删除表, 然后重新运行'db.create_all' 才会重新映射。这样不符合我们的要求,因此flask-migrate就是为了解决 这个问题。它可以在每次修改模型(class)后,可以将修改的字段映射到数据库中 2、首先进入虚拟环境,然后使用‘pip install flask-migrate ’进行安装 3、使用‘flask_migrate’必须借助‘flask_scripts’这个包的'MigrateCommand'中包含了所有和数据库相关 的命令 4、‘flask_migrate’相关的命令: *python manage.py db init :初始化一个迁移脚本的环境,只需要执行一次
*python manage.py db migrate :将模型生成迁移文件,只要模型改变就需要执行
*python manage.py db upgrade :把前一文件真正的映射到数据库中,每次运行了migrate就需要执行该命令 5、注意点:需要将想要映射到数据库中的模型,都要导入到‘manage.py’文件中,如果没有导入,就不会映射到数据库中 6、manage.py 相关代码
manage.py代码:
from flask_script import Manager from migrate_demo import app from flask_migrate import Migrate,MigrateCommand from exts import db from models import Article #init #migrate #upgrade #模型 -->迁移文件 ---> 表 manager = Manager(app) migrate = Migrate(app,db) manager.add_command('db',MigrateCommand) if __name__ == '__main__': manager.run()
config.py 代码:
#encoding:utf-8 #dialect+driver://username:password@host:port/database DIALECT = 'mysql' DRIVER = 'mysqldb' USERNAME = 'root' PASSWORD = 'root' HOST = '127.0.0.1' PORT = '3306' DATABASE = 'migrate_demo' #mysql 不会认识utf-8,而需要直接写成utf8 SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER, USERNAME,PASSWORD,HOST,PORT,DATABASE) SQLALCHEMY_TRACK_MODIFICATIONS = True
exts.py代码:
#encoding:utf-8 from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy()
models.py代码:
#encoding:utf-8 from exts import db class Article(db.Model): __tablename = 'article' id = db.Column(db.Integer,primary_key=True,autoincrement=True) title = db.Column(db.String(100),nullable=False) content = db.Column(db.Text,nullable=False)
migrate_demo.py 代码:
#encoding:utf-8 from flask import Flask from exts import db from models import Article import config app = Flask(__name__) app.config.from_object(config) db.init_app(app) #将这个应用主程序绑定到对应的app # with app.app_context(): #在主程序中,将app推送到app堆栈中;模型与数据库映射 # db.create_all() #如果使用了 flask-migrate 就可以不需要使用这行代码 @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run(debug = True)
有了以上代码后,直接进入manage.py所在目录,执行
E:\PythonProject\migrate_demo>python manage.py db init
Creating directory E:\PythonProject\migrate_demo\migrations ... done
Creating directory E:\PythonProject\migrate_demo\migrations\versions ... done
Generating E:\PythonProject\migrate_demo\migrations\alembic.ini ... done
Generating E:\PythonProject\migrate_demo\migrations\env.py ... done
Generating E:\PythonProject\migrate_demo\migrations\env.pyc ... done
Generating E:\PythonProject\migrate_demo\migrations\README ... done
Generating E:\PythonProject\migrate_demo\migrations\script.py.mako ... done
Please edit configuration/connection/logging settings in 'E:\\PythonProject\\migrate_demo\\migrati
s\\alembic.ini' before proceeding.
E:\PythonProject\migrate_demo>python manage.py db migrate
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'article'
Generating E:\PythonProject\migrate_demo\migrations\versions\b4b05af63024_.py ... done
E:\PythonProject\migrate_demo>python manage.py db upgrade
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> b4b05af63024, empty message
E:\PythonProject\migrate_demo>
说明:
每次models.py修改后,就像以上方式在命令终端中执行命令:
python manage.py db migrate
python manage.py db upgrade
数据库表结构就会做相应的更改,而不用简单除暴的先drop table 表,再运行app主函数来重新映射