Last active
March 5, 2017 16:24
-
-
Save billiepander/cc88f31165d978f1816e5ca940d484f7 to your computer and use it in GitHub Desktop.
flask-migrate example. 1: $ python manage.py init # to create migrations folder 2: $ python manage.py migrate # generate operation file 3:$ python manage.py upgrade # run the file 4&later: run 2, 3 after field s change to makemigrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, request, jsonify | |
| from users import User | |
| from ext import db | |
| app = Flask(__name__) | |
| app.config.from_object('config') | |
| db.init_app(app) | |
| # with app.app_context(): | |
| # db.drop_all() | |
| # db.create_all() | |
| @app.route('/users', methods=['POST']) | |
| def users(): | |
| username = request.form.get('name') | |
| user = User(username) | |
| print('User ID: {}'.format(user.id)) | |
| db.session.add(user) # session对象可以加入多个要创建的数据对象,然后一起使用session.commit()进行提交 | |
| db.session.commit() | |
| return jsonify({'id': user.id, 'name': username}) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=9001) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 内建设置 | |
| DEBUG = True # 启动Flask的Debug模式 | |
| # 扩展设置 | |
| BCRYPT_LEVEL = 13 # 用于Flask-Bcrypt拓展, hash映射密码 | |
| # 自设变量 | |
| MAIL_FROM_EMAIL = "robert@example.com" # 设置邮件来源 | |
| SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://plp@plpblog@localhost/migrateexplore?charset=utf8' | |
| SQLALCHEMY_TRACK_MODIFICATIONS = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask_sqlalchemy import SQLAlchemy | |
| db = SQLAlchemy() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask | |
| from flask_script import Manager | |
| from flask_migrate import Migrate, MigrateCommand | |
| from models import User # take care! need 2 import all the table class! or, it ruin the operation flow | |
| from ext import db | |
| app = Flask(__name__) | |
| app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://plp@plpblog@localhost/migrateexplore?charset=utf8' | |
| db.init_app(app) | |
| migrate = Migrate(app, db) | |
| manager = Manager(app) | |
| manager.add_command('db', MigrateCommand) | |
| if __name__ == '__main__': | |
| manager.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ext import db | |
| class User(db.Model): | |
| __tablename__ = 'users' | |
| id = db.Column(db.Integer, primary_key=True, autoincrement=True) | |
| name = db.Column(db.String(50)) | |
| def __init__(self, name): | |
| self.name = name | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment