展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

flask-migrate迁移ORM模型

  • 安装依赖
(C:\ProgramData\Anaconda3) C:\Users\ychen>pip install flask-migrate
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting flask-migrate
  Downloading https://mirrors.aliyun.com/pypi/packages/93/01/587023575286236f95d2ab8a826c320375ed5ea2102bb103ed89704ffa6b/Flask_Migrate-4.0.7-py3-none-any.whl (21 kB)
Requirement already satisfied: Flask>=0.9 in c:\programdata\anaconda3\lib\site-packages (from flask-migrate) (2.0.1)
Requirement already satisfied: Flask-SQLAlchemy>=1.0 in c:\programdata\anaconda3\lib\site-packages (from flask-migrate) (2.5.1)
  Downloading https://mirrors.aliyun.com/pypi/packages/7e/dc/0099c61985b6cb92688c8b9c9c6943d22b80957751d43c51def582afc546/Flask_Migrate-4.0.6-py3-none-any.whl (21 kB)
  Downloading https://mirrors.aliyun.com/pypi/packages/0e/3a/3e7a97fff0396e5206af5fdcbf66fdbb499b1daa049fbe074566fd73c664/Flask_Migrate-4.0.5-py3-none-any.whl (21 kB)
  Downloading https://mirrors.aliyun.com/pypi/packages/e4/65/9e0a6ce67e1776b0d8a4ebcd40ee519b6c8339a62b05dbc9b153c0d6a17b/Flask_Migrate-4.0.4-py3-none-any.whl (20 kB)
  Downloading https://mirrors.aliyun.com/pypi/packages/7f/cd/ea936f728fd0e2d148ad25dc9859d15c2bf62580ffdbfa04ce4f7aca461a/Flask_Migrate-4.0.3-py3-none-any.whl (20 kB)
  Downloading https://mirrors.aliyun.com/pypi/packages/7e/26/9f3a57830627cf67c26893b7042cb0ec8c77205331636212b81ae3c35f20/Flask_Migrate-4.0.2-py3-none-any.whl (20 kB)
  Downloading https://mirrors.aliyun.com/pypi/packages/26/e1/824c124993ddaac5b5ab30f930d685295c401e1a28d00012f75721edf33d/Flask_Migrate-4.0.1-py3-none-any.whl (20 kB)
Collecting alembic>=0.7
  Downloading https://mirrors.aliyun.com/pypi/packages/b3/e2/8d48220731b7279911c43e95cd182961a703b939de6822b00de3ea0d3159/alembic-1.7.7-py3-none-any.whl (210 kB)
     |████████████████████████████████| 210 kB 1.1 MB/s
Requirement already satisfied: SQLAlchemy>=1.3.0 in c:\programdata\anaconda3\lib\site-packages (from alembic>=0.7->flask-migrate) (1.4.52)
Requirement already satisfied: importlib-metadata in c:\programdata\anaconda3\lib\site-packages (from alembic>=0.7->flask-migrate) (4.8.3)
Collecting Mako
  Downloading https://mirrors.aliyun.com/pypi/packages/b4/4d/e03d08f16ee10e688bde9016bc80af8b78c7f36a8b37c7194da48f72207e/Mako-1.1.6-py2.py3-none-any.whl (75 kB)
     |████████████████████████████████| 75 kB 1.7 MB/s
Requirement already satisfied: importlib-resources in c:\programdata\anaconda3\lib\site-packages (from alembic>=0.7->flask-migrate) (5.4.0)
Requirement already satisfied: click>=7.1.2 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.9->flask-migrate) (8.0.4)
Requirement already satisfied: Jinja2>=3.0 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.9->flask-migrate) (3.0.3)
Requirement already satisfied: itsdangerous>=2.0 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.9->flask-migrate) (2.0.1)
Requirement already satisfied: Werkzeug>=2.0 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.9->flask-migrate) (2.0.3)
Requirement already satisfied: colorama in c:\programdata\anaconda3\lib\site-packages (from click>=7.1.2->Flask>=0.9->flask-migrate) (0.4.5)
Requirement already satisfied: MarkupSafe>=2.0 in c:\programdata\anaconda3\lib\site-packages (from Jinja2>=3.0->Flask>=0.9->flask-migrate) (2.0.1)
Requirement already satisfied: greenlet!=0.4.17 in c:\programdata\anaconda3\lib\site-packages (from SQLAlchemy>=1.3.0->alembic>=0.7->flask-migrate) (2.0.2)
Requirement already satisfied: dataclasses in c:\programdata\anaconda3\lib\site-packages (from Werkzeug>=2.0->Flask>=0.9->flask-migrate) (0.8)
Requirement already satisfied: typing-extensions>=3.6.4 in c:\programdata\anaconda3\lib\site-packages (from importlib-metadata->alembic>=0.7->flask-migrate) (4.1.1)
Requirement already satisfied: zipp>=0.5 in c:\programdata\anaconda3\lib\site-packages (from importlib-metadata->alembic>=0.7->flask-migrate) (3.6.0)
Installing collected packages: Mako, alembic, flask-migrate
Successfully installed Mako-1.1.6 alembic-1.7.7 flask-migrate-4.0.1
  • 导入依赖
from flask_migrate import Migrate
  • 注释如下
# 创建表
# with app.app_context():
#     db.create_all()
  • 配置
db = SQLAlchemy(app)
migrate = Migrate(app, db)
  • 使用
# 先删除mysql中的article表和user表
# 打开pycharm的控制台,初始化操作,只执行1次
(C:\ProgramData\Anaconda3) C:\work\eclipse-workspace\demo>flask db init
c:\programdata\anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by def
ault in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Creating directory C:\work\eclipse-workspace\demo\migrations ...  done
Creating directory C:\work\eclipse-workspace\demo\migrations\versions ...  done
Generating C:\work\eclipse-workspace\demo\migrations\alembic.ini ...  done
Generating C:\work\eclipse-workspace\demo\migrations\env.py ...  done
Generating C:\work\eclipse-workspace\demo\migrations\README ...  done
Generating C:\work\eclipse-workspace\demo\migrations\script.py.mako ...  done
Please edit configuration/connection/logging settings in 'C:\\work\\eclipse-workspace\\demo\\migrations\\alembic.ini' before proceeding.
  • 查看项目文件夹,生成如下文件

  • 同步到数据库

# 识别orm模型,执行如下命令时,mysql数据库中有alembic_version表需先删除,否则会有版本的错误
flask db migrate
# 提交到数据库
flask db upgrade
  • 具体步骤
(C:\ProgramData\Anaconda3) C:\work\eclipse-workspace\demo>flask db migrate
c:\programdata\anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by def
ault in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'user'
INFO  [alembic.autogenerate.compare] Detected added table 'article'
INFO  [alembic.autogenerate.compare] Detected removed table 'j_student'
INFO  [alembic.autogenerate.compare] Detected removed table 'order_details'
INFO  [alembic.autogenerate.compare] Detected removed table 'j_course'
INFO  [alembic.autogenerate.compare] Detected removed table 'j_score'
INFO  [alembic.autogenerate.compare] Detected removed table 'course'
INFO  [alembic.autogenerate.compare] Detected removed table 'j_teacher'
INFO  [alembic.autogenerate.compare] Detected removed table 'teacher'
INFO  [alembic.autogenerate.compare] Detected removed table 'student'
INFO  [alembic.autogenerate.compare] Detected removed table 'score'
INFO  [alembic.autogenerate.compare] Detected removed table 'orders'
INFO  [alembic.autogenerate.compare] Detected removed table 'customers'
Generating C:\work\eclipse-workspace\demo\migrations\versions\a2aad3324414_.py ...  done

(C:\ProgramData\Anaconda3) C:\work\eclipse-workspace\demo>flask db upgrade
c:\programdata\anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by def
ault in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> a2aad3324414, empty message
  • 查看数据库,会将数据库的所有表删除并创建如下3个新的表

  • 在user表中添加1个字段

class User(db.Model):
    __tablename__ = "user"
    # 类型,主键,自增
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # varchar,null=
    username = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    # 添加的字段
    note = db.Column(db.String(100), nullable=False)
  • 执行
(C:\ProgramData\Anaconda3) C:\work\eclipse-workspace\demo>flask db migrate
c:\programdata\anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by def
ault in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added column 'user.note'
Generating C:\work\eclipse-workspace\demo\migrations\versions\7210d316f27b_.py ...  done

(C:\ProgramData\Anaconda3) C:\work\eclipse-workspace\demo>flask db upgrade
c:\programdata\anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:873: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by def
ault in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 9813f4667a99 -> 7210d316f27b, empty message
posted @ 2024-03-20 11:04  DogLeftover  阅读(34)  评论(0编辑  收藏  举报