当前位置:网站首页>(零八)Flask有手就行——数据库迁移Flask-Migrate
(零八)Flask有手就行——数据库迁移Flask-Migrate
2022-07-24 03:53:00 【自由小冰儿】
零、前言
本篇文章,阐述一下Flask中数据库的迁移

为什么要说数据库迁移呢?
比如我们以前有一个数据库,里面的信息有id, name现在我想再加一个gender属性,应该怎么办呢?不可能直接把数据库删除掉吧。然后重新创建一个,因此本文介绍一种,通过数据库迁移的方法,可以保留原始的数据,并完成添加新的一列的方法。
安装
直接使用pip install flask-migrate即可
初始化
项目结构
# exts.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
老样子,我们还是在exts.py中,初始我们的数据库,这里时放Flask扩展的地方。
# models.py
from exts import db
class User(db.Model):
id = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String(80),unique=True)
def __repr__(self):
return '<User %s>' % self.username
models.py是放模型的地方。
# app.py
from flask import Flask
from exts import db
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///foo.db"
# !!!绑定app和数据库
db.init_app(app)
migrate = Migrate(app,db)
if __name__ == '__main__':
app.run()
主程序,我们在!!!进行初始化数据库迁移的migrate 对象。初始化之后我们可以执行下面的三大命令去创建我们的数据库。就不需要db.create_all()命令啦。
三大命令
# (1)初始化
flask db init
# 把当前的模型添加到迁移文件
flask db migrate
# 将映射文件真正的映射到数据库中
flask db upgrade
- (1)新建一个名字为migrations的文件夹,并且记录一个数据库版本号
其他问题:
- 如果报错
[flask_migrate] Error: Can‘t locate revision identified by '409392ed6301'
答:db revision --rev-id 409392ed6301409392ed6301是问题的版本号- Error: While importing ‘app’, an ImportError was raised.
答:请先去运行一下app.py,确保app.py可以运行- Error: Could not locate a Flask application. You did not provide the “FLASK_APP” environment variable, and a “wsgi.py” or “app.py” module was not found in the current directory.
答:请确保执行命令时的文件夹中有app.py文件

边栏推荐
- DOM related method concepts
- MLP-多层感知机
- PAT甲级 1041 Be Unique
- Successfully solved: error: SRC refspec master doors not match any
- Yu zhirs] below refers to the return structure push sent to the remote terminal
- Pat grade a 1043 is it a binary search tree
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 1 (summary of some topics)
- Idea failed to load resource: the server responded with a status of 404 (not found)
- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
- Preliminary exploration of Flink principle and flow batch integration API (II) V2
猜你喜欢

Histone research -- Characteristics and literature references of histones in Worthington calf thymus

C language classic exercises (2) - "bubble sort"“

Convert the pseudo array returned by childNodes into a true array

Pit encountered in project upgrading

Shengsi YiDianTong | deep learning analysis of classical convolutional neural network

【云原生】快速了解Kubernetes

RTOS internal skill cultivation (10) | in depth analysis of RTOS kernel context switching mechanism

Pat grade a 1041 be unique

buu web

硬件知识3--IIC协议
随机推荐
Scenario and value of data desensitization [summary]
Introduction to JVM class loading process
【云原生】快速了解Kubernetes
三菱转以太网模块远创智控YC8000-FX 连接 MCGS操作方法
硬件知识3--IIC协议
High precision phase shift (mcp41xx) program STM32F103, f407 are common, just change the pin (SPI software analog communication)
Active vibration reduction system of hub motor and its vertical performance optimization
Embedded system transplantation [5] - Cross compilation tool chain
Emqx v4.4.5 Publishing: new exclusive subscriptions and mqtt 5.0 publishing attribute support
Matlab sound signal processing frequency diagram signal filtering and playing sound
Redis
93. (leaflet chapter) leaflet situation plotting - modification of attack direction
Qt ROS相关操作(运行终端指令、发布订阅自定义消息话题或服务、订阅图像并显示)
How to protect JDBC applications from SQL injection
Mongo from start to installation and problems encountered
Solution to the problem of "out of sight, out of mind, out of order" in the training competition
Prosci Lag3 antibody: improve in vitro research and help cancer immunotherapy
oh-my-zsh
Appendtofile append failed
Algorithm interview high frequency problem solving guide [1]