First, we introduce the problem of circular referencing:
When a module needs to refer to a class of another module, and another module needs to refer to the module's class, there is a circular reference, and cannot import the class, this time can cut off one of the reference path, add a module
Project structure:
models_sep.py Code:
fromFlaskImportFlask fromModelsImportarticle fromExtsImportDBImportConfigapp= Flask (__name__) app.config.from_object (config) db.init_app (app)#Db.create_all ()@app. Route ('/')defHello_world ():return 'Hello world!'if __name__=='__main__': App.run (Debug=true)
models.py Code:
from Import DB class article (db. Model): __tablename'article' = db. Column (db. integer,primary_key=true,autoincrement=True) = db. Column (db. String (+), Nullable=false)
exts.py Code:
from Import = SQLAlchemy ()
# # # separate ' models ' and resolve circular references:
1. The purpose of separate models: in order to make the code more convenient management.
2. How to resolve circular references: put ' db ' in a separate file, cut off the loop-referenced line.
Then, talk about model initialization init, model migration migrate and model upgrade
To solve the problem: if the first time to create a good data model, then to change the model structure, add and remove changes and so on, the rough way is to delete the entire table, and then new, but this is very detrimental to maintenance, so it is necessary to use the Flask_migrate
Project structure:
migrate_demo.py Code:
fromFlaskImportFlask fromExtsImportDBImportConfig fromModelsImportArticleapp= Flask (__name__) app.config.from_object (config) db.init_app (app)#Create a new article model, using models separate way#flask-scripts@app. Route ('/')defHello_world ():return 'Hello world!'if __name__=='__main__': App.run (Debug=true)
manage.py File Code:
fromFlask_scriptImportManager fromMigrate_demoImportapp fromFlask_migrateImportMigrate,migratecommand fromExtsImportDB fromModelsImportarticle#Init#Migrate#Upgrade#table, migration file, modelManager=Manager (APP)#1. To use Flask_migrate, you must bind the app and DBMigrate =Migrate (app,db)#2. Add the Migratecommand command to the managerManager.add_command ('DB', Migratecommand)if __name__=='__main__': Manager.run ()
exts.py file code;
from Import = SQLAlchemy ()
models.py File Code:
from Import DB class article (db. Model): __tablename__'article' = db. Column (db. integer,primary_key=true,autoincrement=True) = db. Column (db. String (+), nullable=False) = db. Column (db. text,nullable=False) = db. Column (db. String (+), nullable=False) = db. Column (db. String (a), nullable=false)
In the project file path of the terminal, run in sequence:
Python manage.py DB init
Python manage.py db Migrate
Python manage.py DB Upgrade
found that the article table has been created in the database
Then add a data field authors to the data model in the models.py file, then run Python manage.py db migrate and Python manage.py DB upgrade in the terminal
You can use the Discovery database table to have the article modified:
Introduction and installation of # # # Flask-migrate:
1. Introduction: Because the use of ' Db.create_all ' in the late modification of the field, will not automatically map to the database, the table must be deleted, and then rerun ' Db.craete_all ' will not be re-mapped, this does not meet our needs. So Flask-migrate is trying to solve this problem, she can map the changes to the database every time the model is modified.
2. First enter into your virtual environment, then use ' pip install flask-migrate ' to install it.
3. Use ' flask_migrate ' must be ' flask_scripts ', this package's ' Migratecommand ' contains all the database-related commands.
4. ' Flask_migrate ' related commands:
* ' Python manage.py db init ': Initializes an environment for a migration script that only needs to be performed once.
* ' Python manage.py db migrate ': Generate a migration file for the model, as long as the model changes, you need to execute this command again.
* ' Python manage.py db upgrade ': The migrated file is really mapped to the database. Once you run the ' Migrate ' command, you'll remember to run the command.
5. Note: The model you want to map to the database needs to be imported into the ' manage.py ' file and will not be mapped to the database if it is not imported.
6. ' manage.py ' related code:
```
From Flask_script import Manager
From Migrate_demo Import app
From flask_migrate import Migrate,migratecommand
From exts Import db
From models Import article
# init
# Migrate
# Upgrade
# migration file, model, table
Manager = Manager (APP)
# 1. To use Flask_migrate, you must bind the app and DB
Migrate = Migrate (APP,DB)
# 2. Add the Migratecommand command to the manager
Manager.add_command (' db ', Migratecommand)
if __name__ = = ' __main__ ':
Manager.run ()
```
Flask Web Development Road Nine