前提: settings.py已经连接数据库
两张表想要互相调用 必须定义主外键约束,外键定义在多的一方
(1). 新建 apps / user / models.py 创建用户相关模型类
from datetime import datetime
from ext import dbclass User(db.Model):# 用户表id = db.Column(db.Integer, primary_key=True, autoincrement=True)username = db.Column(db.String(15), nullable=False)password = db.Column(db.String(128), nullable=False)phone = db.Column(db.String(11), unique=True, nullable=False)email = db.Column(db.String(30))icon = db.Column(db.String(100))isdelete = db.Column(db.Boolean, default=False)radtetime = db.Column(db.DateTime, default=datetime.now)# 代码层次: 不需要迁移,只要建立外键,就需要加relationship# 增加一个字段:db.relationship("关联的外键信息表","反向引用:外键能关联到主键信息表")articles = db.relationship("Article", backref='user')comments = db.relationship('Comment', backref="user")def __str__(self):return self.usernameclass Photo(db.Model):# 相册表id = db.Column(db.Integer, primary_key=True, autoincrement=True)photo_name = db.Column(db.String(50), nullable=False)photo_datetime = db.Column(db.DateTime, default=datetime.now)user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)def __str__(self):return self.photo_nameclass AboutMe(db.Model):# 关于我页面id = db.Column(db.Integer, primary_key=True, autoincrement=True)content = db.Column(db.BLOB, nullable=False)pdatetime = db.Column(db.DateTime, default=datetime.now)# 要与用户建立联系user_id = db.Column(db.Integer, db.ForeignKey('user.id'), unique=True)user = db.relationship("User", backref="about")class MessageBoard(db.Model):# 留言id = db.Column(db.Integer, primary_key=True, autoincrement=True)content = db.Column(db.String(255), nullable=False)mdatetime = db.Column(db.DateTime, default=datetime.now)user_id = db.Column(db.Integer, db.ForeignKey('user.id'))# 关系:user = db.relationship('User', backref='messages')
(2). 新建 apps / articie/ models.py 创建文章相关模型类
(1) . Article 中的 content字段 需要在数据库类型手动改为 blob类型
from datetime import datetime
from ext import dbclass Article_type(db.Model):# 文章分类# 生成表的时候提示错误? 可能是因为外键绑定的id内容不存在,还不允许为空# __tablename__ = "type"id = db.Column(db.Integer, primary_key=True, autoincrement=True)type_name = db.Column(db.String(20), nullable=False)articles = db.relationship("Article", backref='article_type')class Article(db.Model):# 文章表id = db.Column(db.Integer, primary_key=True, autoincrement=True)title = db.Column(db.String(50), nullable=False)content = db.Column(db.Text, nullable=False)pdatetime = db.Column(db.DateTime, default=datetime.now())click_num = db.Column(db.Integer, default=0)save_num = db.Column(db.Integer, default=0)love_num = db.Column(db.Integer, default=0)# 文章定义用户外键user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)# 文章定义分类外键type_id = db.Column(db.Integer, db.ForeignKey('article_type.id'), nullable=False)# 只是通过文章找评论表中的评论内容comments = db.relationship("Comment", backref="article")class Comment(db.Model):# 评论表# 数据库默认表名为小写类名,自定义表名# __tablename__ = 'comment'id = db.Column(db.Integer, primary_key=True, autoincrement=True)comment = db.Column(db.String(255), nullable=False)user_id = db.Column(db.Integer, db.ForeignKey('user.id'))article_id = db.Column(db.Integer, db.ForeignKey('article.id'))cdatetime = db.Column(db.DateTime, default=datetime.now)def __str__(self):return self.comment
(3). 将模型引入app.py中
from apps.user.models import *
from apps.articie.models import *
(4). 指令生成模型,迁移数据库
python app.py db init
python app.py db migrate
python app.py db upgrade