当前位置:网站首页>Flask框架中如何进行数据库的迁移
Flask框架中如何进行数据库的迁移
2022-07-23 23:20:00 【小脑斧ai吃肉】
Flask框架中如何进行数据库的迁移
Flask框架中进行数据的迁移需要安装Flask-Migrate插件
安装命令:pip install flask-migrate
定义ORM模型
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
# 数据库的配置变量
HOSTNAME = '127.0.0.1'
PORT = '3306'
DATABASE = 'zzh_flask'
USERNAME = 'root'
PASSWORD = 'root'
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
# 将这个定义好的数据库连接字符串DB_URI,通过`SQLALCHEMY_DATABASE_URI`这个key名配置到`app.config`中。
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
# 设置是否每次数据库修改都跟踪
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# 使用`flask_sqlalchemy.SQLAlchemy`这个类定义一个对象,并将`app`传入进去。
db = SQLAlchemy(app)
# 创建一个migrate对象
migrate = Migrate(app, db)
# 定义ORM模型
class User(db.Model):
__talblename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(200), nullable=False)
class Article(db.Model):
__tablename__ = 'article'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(200), nullable=False)
content = db.Column(db.Text, nullable=False)
# 外键
# 1.外键的数据类型一定要看,所引用的字段的类型
# 2.db.ForeignKey("表名.字段名")
# 3.外键是属于数据库层面的,不推荐直接在ORM中使用
# db.ForeignKey声明这个字段是外键
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
# relationship
# 1.第一个参数是模型的名字,必须要和模型的名字保持一致
# 2.backref(back reference):代表反向引用,"articles"代表对方访问我的时候的字段名称,前提是在前面已经关联了一个外键
# 这里的作用就是通过作者访问他的所有文章,例如user.articles
author = db.relationship("User", backref="articles")
class UserExtension(db.Model):
__tablename__ = 'user_extension'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
school = db.Column(db.String(100))
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
# db.backref
# 1.在反向应用的时候需要传递一些其他的参数,那么就需要用到这个函数,如果不需要使用,只要在relationship的backref参数上,设置反向引用的名称就可以了
# 2.uselist=False代表反向引用的时候,不是一个列表,而是一个对象,就是只能查到一个,一对一
user = db.relationship("User", backref=db.backref("extension", uselist=False))
if __name__ == '__main__':
app.run()
进行模型迁移
①flask db init
②flask db migrate
③flask db upgrade
其中第二步flask db migrate时可以写成flask db migrate -m "first commit",这样写是可以在迁移脚本的时候注明修改了什么。添加的备注会在迁移脚本中体现
当ORM模型被修改之后,例如我给User添加了一个password字段,那如何迁移
,此时就不要init了,直接 flask db migrate -m "add password",flask db upgrade即可
参考:https://www.zlkt.net/book/detail/10/297
边栏推荐
- Tap series article 4 | backstage based tap developer portal
- FreeRTOS personal notes - create / delete dynamic tasks, start scheduler
- YOLOv4: Optimal Speed and Accuracy of Object Detection
- [laser principle and Application-8]: EMC design of laser circuit
- [C language] address book (static version)
- 砺夏行动 2022|源启数字化圆桌论坛即将上线
- Analysis of mobile semantics and perfect forwarding
- Series of articles | the way to advance the microservice architecture in the cloud native era - best practices of microservice splitting
- Contemporary inspirational "women"
- Grey prediction (matlab)
猜你喜欢

2、 Digital logic functional unit

Lin Zhiying's injury is relatively stable

Software architecture

FL Studio 20.9 update Chinese version host Daw digital audio workstation

TAP 系列文章8 | TAP 学习中心——通过动手教程来学习

H7-tool serial port offline burning operation instructions, support TTL serial port, RS232 and RS485 (2022-06-30)

Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?

BGP基础实验

D1-h development board - Introduction to Nezha development

作为开发,你不得不知道的三个性能测试工具|Jmeter、Apipost、JMH使用指南
随机推荐
The role of physical layer, link layer, network layer, transport layer and application layer of tcp/ip model of internet protocol stack
13. Roman to integer
Strncat () strncmp ()
The canfd/can interface offline burning operation instructions of h7-tool have been updated (2022-07-12)
[tensorflow] check whether tensorflow GPU is available
系列文章|云原生时代下微服务架构进阶之路 - 微服务拆分的最佳实践
USB Foundation
EasyNVR平台如何关闭匿名登录?
Merge.Avhd into.Vhd manually in Hyper-V
anchor free yolov1
[jailhouse article] a novel software architecture for mixed criticality systems (2020)
Lixia action | Yuanqi Digitalization: existing mode or open source innovation?
strncat() strncmp()
Video Number strengthens the fight against vulgar content: the content that violates public order and good customs must be eliminated
【Unity3D日常BUG】Unity3D解决“找不到类型或命名空间名称“XXX”(您是否缺少using指令或程序集引用?)”等问题
(CVPR-2022)BiCnet
At the forefront of the times, Huawei aims at the wind and sea of digital finance
SecureCRT garbled
[web vulnerability exploration] SQL injection vulnerability
Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework