config.js
HOSTNAME = '192.168.195.161'
PORT = '3306'
DATABASE = 'medicine_settle'
USERNAME = 'admin'
PASSWORD = 'admin'
DB_URI = 'mysql+mysqldb://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE)
SQLALCHEMY_DATABASE_URI = DB_URI
SQLALCHEMY_TRACK_MODIFICATIONS = True
exts.js
#encoding: utf-8
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
manage.py
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from exts import db
from migrate_demo import app
# python manage.py db init
# python manage.py db migrate
# python manage.py db upgrade
# 模型 -> 迁移文件 -> 表
manager = Manager(app)
# 1. 要使用flask_migrate,必须绑定app和db
migrate = Migrate(app, db)
# 2. 把MigrateCommand命令添加到manager中
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
migrate_demo.py
#encoding: utf-8
from flask import Flask
from exts import db
import config
app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
# 新建一个article模型,采用models分开的方式
# flask-scrpts的方式
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
models.py
#encoding: utf-8
from .exts import db
class Article(db.Model):
__tablename__ = 'article'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
title = db.Column(db.String(100),nullable=False)
content = db.Column(db.Text,nullable=False)
tags = db.Column(db.String(100),nullable=False)