FLASK入门Flask

Flask-数据库常见模版代码

2018-06-15  本文已影响3人  梦醒家先生

一对多

class Role(db.Model):
    """角色表"""
    __tablename__ = 'roles'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    # 一的一方定义关系引用
    users = db.relationship('User', backref='role', lazy='dynamic')

class User(db.Model):
    """用户表"""
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True, index=True)
    # 多的一方定义外键
    role_id = db.Column(db.Integer, db.ForeignKey(Role.id))

多对多

# 多对多的中间表的定义其实--->2个表的外键
student_course = db.Table('student_course',
                         db.Column('course_id',db.Integer, db.ForeignKey('courses.id')),
                        db.Column('student_id ',db.Integer, db.ForeignKey('students.id'))
# 定义模型类
class Student(db.Model):
    __tablename__ = 'students'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    # course_id=db.Column(db.Integer, db.ForeignKey('courses.id'))
    # 定义一个关系引用和反向引用
    courses = db.relationship('Course', 
                          secondary=student_course,
                          backref=db.backref('students', lazy='dynamic'),
                          lazy='dynamic')
  
class Course(db.Model):
    __tablename__ = 'courses'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    # student_id = db.Column(db.Integer, db.ForeignKey('students.id'))

自关联一对多

class Comment(db.Model):
   """评论"""
   __tablename__ = "comments"

   id = db.Column(db.Integer, primary_key=True)
   # 评论内容
   content = db.Column(db.Text, nullable=False)
   # 父评论id
   parent_id = db.Column(db.Integer, db.ForeignKey("comments.id"))
   # 父评论(也是评论模型)
   parent = db.relationship("Comment", remote_side=[id],
                            backref=db.backref('childs', lazy='dynamic'))

# 测试代码
if __name__ == '__main__':
   db.drop_all()
   db.create_all()

   com1 = Comment(content='我是主评论1')
   com2 = Comment(content='我是主评论2')
   com11 = Comment(content='我是回复主评论1的子评论1')
   com11.parent = com1
   com12 = Comment(content='我是回复主评论1的子评论2')
   com12.parent = com1

   db.session.add_all([com1, com2, com11, com12])
   db.session.commit()
   app.run(debug=True)

自关联多对多

tb_user_follows = db.Table(
    "tb_user_follows",
    db.Column('follower_id', db.Integer, db.ForeignKey('info_user.id'), primary_key=True),  # 粉丝id
    db.Column('followed_id', db.Integer, db.ForeignKey('info_user.id'), primary_key=True)  # 被关注人的id
)

class User(db.Model):
    """用户表"""
    __tablename__ = "info_user"

    id = db.Column(db.Integer, primary_key=True)  
    name = db.Column(db.String(32), unique=True, nullable=False)

    # 用户所有的粉丝,添加了反向引用followed,代表用户都关注了哪些人
    followers = db.relationship('User',
                                secondary=tb_user_follows,
                                primaryjoin=id == tb_user_follows.c.followed_id,
                                secondaryjoin=id == tb_user_follows.c.follower_id,
                                backref=db.backref('followed', lazy='dynamic'),
                                lazy='dynamic')
上一篇下一篇

猜你喜欢

热点阅读