flask-sqlalchemy,flask-migrate
flask-sqlalchemy使用
集成到flask中,直接使用sqlalchemy,
有个第三方flask-sqlalchemy,可以快速的集成到flask中
使用flaks-sqlalchemy集成
# 集成到flask中,直接使用sqlalchemy,看代码
# 有个第三方flask-sqlalchemy,帮助咱们快速的集成到flask中
# 使用flask-sqlalchemy集成
1 导入 from flask_sqlalchemy import SQLAlchemy
2 实例化得到对象
db = SQLAlchemy()
3 将db注册到app中
db.init_app(app)
4 视图函数中使用session
全局的db.session # 线程安全的
5 models.py 中继承Model
db.Model
6 写字段
username = db.Column(db.String(80), unique=True, nullable=False)
7 配置文件中加入
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root@127.0.0.1:3306/ddd?charset=utf8"
SQLALCHEMY_POOL_SIZE = 5
SQLALCHEMY_POOL_TIMEOUT = 30
SQLALCHEMY_POOL_RECYCLE = -1
# 追踪对象的修改并且发送信号
SQLALCHEMY_TRACK_MODIFICATIONS = False
------------------------------------------------
setting.py
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
class Setting:
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:root@127.0.0.1:3306/ddd?charset=utf8"
SQLALCHEMY_POOL_SIZE = 5
SQLALCHEMY_POOL_TIMEOUT = 30
SQLALCHEMY_POOL_RECYCLE = -1
# 追踪对象的修改并且发送信号
SQLALCHEMY_TRACK_MODIFICATIONS = False
app = Flask(__name__)
app.config.from_object(Setting())
# 实例化得到对象
db = SQLAlchemy()
# 将db注册到app中
db.init_app(app)
----
main.py
from settings import app
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
flask-migrate使用
表发生变化,都会记录,自动同步到数据库中
原生的sqlalchemy,不支持修改表的
flask-migrate可以实现类似于django的
python manage.py makemigrations #记录
python manage.py migrate #真正的同步到数据库、
使用步骤:
版本用这个flask:2.2.2 flask-script:2.0.3
安装flask-script
pip3.8 install flask-migrate==2.7.0
在app所在的py文件中
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)
manager.run() # 以后使用python manage.py runserver 启动项目
以后第一次执行一下
python manage.py db init # 生成一个migrations文件夹,里面以后不要动,记录迁移的编号
以后再models.py写表,加字段,删字段,改参数
只需要执行
python manage.py db migrate # 记录
python manage.py db upgrade # 真正的同步进去
manage.py
from settings import app, db
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
# 必须要导入一下才能被监控到
# from models import User
manage = Manager(app)
Migrate(app, db)
manage.add_command('db', MigrateCommand)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
manage.run()
settings.py
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
class Setting:
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:root@127.0.0.1:3306/ddd"
SQLALCHEMY_POOL_SIZE = 5
SQLALCHEMY_POOL_TIMEOUT = 30
SQLALCHEMY_POOL_RECYCLE = -1
# 追踪对象的修改并且发送信号
SQLALCHEMY_TRACK_MODIFICATIONS = False
app = Flask(__name__)
app.config.from_object(Setting())
# 实例化得到对象
db = SQLAlchemy()
# 将db注册到app中
db.init_app(app)
# 导入模型方便执行迁移的时候可以管理到
from models import *
models.py
from settings import db
from sqlalchemy import Integer, Column
# 需要自己手动迁移
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32))
class Pool(db.Model):
__tablename__ = 'pool'
id = db.Column(db.Integer, primary_key=True)
pool_name = db.Column(db.String(32), default='123')