首先安装flask-sqlalchemy
db = SQLAlchemy(app)
一 Mmeber、User模型类的创建
# coding: utf-8
from app import db, app
class Member(db.Model):
__tablename__ = 'member'
id = db.Column(db.Integer, primary_key=True)
membername = db.Column(db.String(100), unique=True, index=True, info='会员名')
mobile = db.Column(db.String(11), server_default=db.FetchedValue(), info='会员手机号码')
sex = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='性别 1:男 2:女')
avatar = db.Column(db.String(200), nullable=True, server_default=db.FetchedValue(), info='会员头像')
salt = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue(), info='随机salt')
reg_ip = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue(), info='注册ip')
status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue(), info='状态 1:有效 0:无效')
updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue(), info='最后一次更新时间')
created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue(), info='插入时间')
@property
def status_desc(self):
return app.config['STATUS_MAPPING'][str(self.status)]
@property
def sex_desc(self):
sex_mapping = {
"0":"未知",
"1":"男",
"2":"女"
}
return sex_mapping[str(self.sex)]
#status 统一返回sex表格的字段, 虚拟的好处是 直接可以在index.html中直接使用。 虚拟字段不可以查询
# coding: utf-8
from app import db
class User(db.Model):
__tablename__ = 'user'
uid = db.Column(db.BigInteger, primary_key=True)
nickname = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
mobile = db.Column(db.String(20), nullable=False, server_default=db.FetchedValue())
email = db.Column(db.String(100), nullable=False, server_default=db.FetchedValue())
sex = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
avatar = db.Column(db.String(64), nullable=False, server_default=db.FetchedValue())
login_name = db.Column(db.String(20), nullable=False, unique=True, server_default=db.FetchedValue())
login_pwd = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
login_salt = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue())
status = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
二 app.py中导入User,Member模型类
from common.models.member import Member
from common.models.User import User
三: 在终端代码中给数据库创建对应的Member表格 和User表格
首先进入flask shell,然后使用创建代码
db.create_all()