In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to do database migration in Flask. It is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.
# pip install flask-migrate
#coding=utf-8
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate,MigrateCommand
from flask_script import Shell,Manager
app = Flask(name)
manager = Manager(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@127.0.0.1:3306/Flask_test'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
#The first argument is an instance of Flask, the second argument is an instance of Sqlalchemy database
migrate = Migrate(app,db)
#manager is an instance of Flask-Script, which adds a db command to Flask-Script
manager.add_command('db',MigrateCommand)
#Define the Role of the model
class Role(db.Model):
#Define table name
__tablename__ = 'roles'
#Define column objects
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
user = db.relationship('User', backref='role')
#repr() method displays a readable string,
def __repr__(self):
return 'Role:'.format(self.name)
#Define user
class User(db.Model):
__talbe__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
#Set foreign key
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
def __repr__(self):
return 'User:'.format(self.username)
if __name__ == '__main__':
manager.run()
```
#The following are the operations performed at the terminal
Create Migration Repository #This command creates the migrations folder where all migration files will be placed. python database.py db init Create migration script
There are two functions to automatically create migration scripts
upgrade(): The function applies changes made during migration to the database.
downgrade(): The function removes the change.
An automatically created migration script generates the contents of the upgrade() and downgrade() functions based on differences between the model definition and the current state of the database.
The comparison may not be completely correct, and some details may be missed, which needs to be checked.
python database.py db migrate -m 'initial migration' update database python database.py db upgrade return previous version
You can find the version number by following the history command and pass it to the downgrade command:
python app.py db history Output format: -> version number (head), initial migration
Rollback to a specified version
python app.py db downgrade version number actual operation order:
1. Python file db init
2. python file db migrate -m"Version name (comments)"
3. Python file db upgrade and then observe the table structure
4. Modify the model according to requirements
5. python file db migrate -m"New version name (comments)"
6. Python file db upgrade and then observe the table structure
7. If the version is returned, use the python file db history to view the version number
8. python file db downgrade(upgrade) version number
That's how database migration works in Flask. Have you learned anything or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.