1 Introduction
Flask-Migrate is an extension that handles SQLAlchemy database migration for Flask applications, making it possible to operate the database through Flask's command line interface or Flask-Scripts.
2. Use
flask db history 查看历史迁移信息
flask db current 查看当前数据库版本
flask db init 初始化数据库
flask db migrate -m "init_database" 数据库迁移
flask db upgrade 更新数据库至最新版本
flask db upgrade revision_id 更新数据库至某一版本revision_id
flask db downgrade 回退一个版本
flask db downgrade revision_id 回退至某一版本revision_id
3. Automatically change the table field type and field length
Recently, in the development process, I encountered the problem of converting the string type to the bool type. However, the design of the table was designed to be the string type at the beginning, so record the way that flask-migrate changes the table field type.
Alembic supports the detection of field length changes, but it is not the default and needs to be configured;
find the migrations/env.py file, and add the following content to the run_migrations_online function:
context.configure(
…………
compare_type=True, # 检查字段类型
compare_server_default=True # 比较默认值
)