class Students(BaseModel): """ 学员表 """ __tablename__ = "students" name = models.Column(models.String(32)) age = models.Column(models.Integer) gender = models.Column(models.Integer,default = 13) #0 男 1女 -1 unknown
class Teacher(BaseNodel): #老师表 __tablename__ = 'teacher' name = models.Column(models.String(32)) age = models.Column(models.Integer) gender = models.Column(models.Integer) # 0 男 1女 -1 unknown course_id = models.Column(models.Integer, models.ForeignKey("course.id"))#外键字段的搭建对应的课程表
class Teachers(BaseModel): """ 教师 老师与课程是多对一关系 """ __tablename__ = "teachers" name = models.Column(models.String(32)) age = models.Column(models.Integer) gender = models.Column(models.Integer,default = 3) # 0 男 1女 -1 unknown course_id = models.Column(models.Integer, models.ForeignKey("course.id")) #教师多对一 映射表是课程
class Student(BaseNodel): #学生表 __tablename__ = 'students' name = models.Column(models.String(32)) age = models.Column(models.Integer) gender = models.Column(models.Integer)#0男 1女 to_attendance = models.relationship( 'Attendance',#考勤的方法名 backref = 'to_student_hh'#反向字段 )
class Course(BaseNodel): #课程表 __tablename__ = 'course' label = models.Column(models.String(32)) description = models.Column(models.Text) to_teacher = models.relationship( 'Teacher',#隐射 backref = 'to_course_data'#反向字段teacher 查询课程的字段 ) to_student = models.relationship( 'Student',#方法名 secondary = Stu_Cou, backref = models.backref('to_course',lazy = 'dynamic'),#反向字段(stu_cou studens) lazy = 'dynamic'#(stu_cou course) # select 访问该字段时候,加载所有的映射数据 # joined 对关联的两个表students和stu_cou进行join查询 # dynamic 不加载数据 )
class Course(BaseModel): """ 课程表 """ __tablename__ = "course" label = models.Column(models.String(32)) description = models.Column(models.Text) to_teacher = models.relationship( "Teachers", #映射表 backref = "to_course_data", #反向映射字段,反向映射表通过该字段查询当前表内容 ) #指向映射表字段 to_student = models.relationship( "Students", secondary = Stu_Cou, backref = models.backref("to_course",lazy = "dynamic"), #stu_cou course lazy = "dynamic" #stu_cou student #select 访问该字段时候,加载所有的映射数据 #joined 对关联的两个表students和stu_cou进行join查询 #dynamic 不加载数据 )
class User(BaseModel): __tablename__ = "user" username = models.Column(models.String(32)) password = models.Column(models.String(32)) identity = models.Column(models.Integer) #0 学员 #1 教师 identity_id = models.Column(models.Integer,nullable=True)