import json
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import MigrateCommand, Migrate
from flask_script import Manager
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@127.0.0.1:3306/tdms'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy()
db.init_app(app)
manager = Manager(app) # 创建启动命令管理对象
Migrate(app, db) # 创建数据库迁移工具对象
manager.add_command("mysql", MigrateCommand) # 向启动命令管理对象中添加迁移命令
class MenuTree(db.Model):
'''
自关联 模型
menu_tree | CREATE TABLE `menu_tree` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`description` varchar(500) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `parent_id` (`parent_id`),
CONSTRAINT `menu_tree_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `menu_tree` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
'''
__tablename__ = 'menu_tree'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
description = db.Column(db.String(500))
parent_id = db.Column(db.Integer, db.ForeignKey('menu_tree.id'))
parent = db.relationship("MenuTree", remote_side=[id])
def to_dict(self):
resp_dict = {
"id": self.id,
# "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
"description": self.description,
"parent": self.parent.to_dict() if self.parent else None,
"name": self.name,
}
return resp_dict
# 执行命名
# python3 manage.py mysql init
# python3 manage.py mysql migrate -m '描述'
# python3 manage.py mysql upgrade
if __name__ == '__main__':
manager.run()
网友评论