SQLAlchemy中使用sqlacodegen
flask-sqlalchemy中使用flask-sqlacodegen --flask
flask-sqlalchemy它致力于简化在 Flask 中 SQLAlchemy 的使用,提供了有用的默认值和额外的助手来更简单地完成常见任务。
## 自己的代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Generate models from database
Created date: 2018/12/05
Author: Bryan Yang
"""
import os
def gen_models():
db_url = "mysql://root:root@localhost:3306/Plants?charset=utf8"
plants_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
model_path = os.path.join(plants_path, 'plants_app/models/models.py')
cmd = 'flask-sqlacodegen --flask {}'.format(db_url)
try:
output = os.popen(cmd)
content = str(output.read())
with open(model_path, 'w+') as f:
f.write(content)
print('Generated database models successfully')
except Exception, e:
print e
if __name__ == '__main__':
gen_models()