class PostComment(db.Model): id = db.Column(db.String(32), primary_key=True) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) # 评论时间 body = db.Column(db.Text) # 评论内容 post_id = db.Column(db.String(32), db.ForeignKey('post.id')) # 文章ID-外键 post = db.relationship('Post', back_populates='post_comments') # 文章-反向关联
class TbMessage(db.Model): id = db.Column(db.String(40), primary_key=True) title = db.Column(db.String(20)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) def __repr__(self): return "<Message body %r>" % self.body
class Post(db.Model): id = db.Column(db.String(32), primary_key=True) title = db.Column(db.String(100)) #文章标题 body = db.Column(db.Text) #文章内容 timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) #发表时间 category_id = db.Column(db.String(32), db.ForeignKey('category.id')) #所属分类ID-外键 category = db.relationship('Category', back_populates='posts') #所属分类-反向关联 comments = db.relationship('Comment', back_populates='post', cascade='all') #文章评论-反向关联 post_comments = db.relationship('PostComment', back_populates='post', cascade='all') #文章简单评论-反向关联
class Comment(db.Model): id = db.Column(db.String(32), primary_key=True) author = db.Column(db.String(30)) #作者 email = db.Column(db.String(254)) #邮箱 site = db.Column(db.String(255)) #个人网址 body = db.Column(db.Text) #评论内容 from_admin = db.Column(db.Boolean, default=False) #来着博主 reviewed = db.Column(db.Boolean, default=False) #已审核 timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) #发表时间 post_id = db.Column(db.String(32), db.ForeignKey('post.id')) #文章ID-外键 post = db.relationship('Post', back_populates='comments') #文章-反向关联 comment_id = db.Column(db.String(32), db.ForeignKey('comment.id')) #评论ID-自身外键 replied = db.relationship('Comment', back_populates='replies', remote_side=[id]) #父评论-反向关联 replies = db.relationship('Comment', back_populates='replied', cascade='all') #子评论-反向关联
class TbArticle(db.Model): id = db.Column(db.String(40), primary_key=True) title = db.Column(db.String(20)) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) #设置外键:键值为另一侧的表名和主键字段名 author_id = db.Column(db.String(40), db.ForeignKey('tb_author.id')) def __repr__(self): return "<Article %s>" % self.title association_table = db.Table( 'association', db.Column('student_id', db.String(40), db.ForeignKey('student.id')), db.Column('teacher_id', db.String(40), db.ForeignKey('teacher.id'))) class Student(db.Model): id = db.Column(db.String(40), primary_key=True) name = db.Column(db.String(50), unique=True) grade = db.Column(db.String(10)) clazz = db.Column(db.String(10)) teachers = db.relationship('Teacher', secondary=association_table, back_populates='students') class Teacher(db.Model): id = db.Column(db.String(40), primary_key=True)