from sqlalchemy import Column, Integer, String, DateTime, Boolean, Text, DECIMAL, ForeignKey, Index, UniqueConstraint from sqlalchemy.orm import relationship from datetime import datetime from database import Base class BaseModel: id = Column(Integer, primary_key=True, autoincrement=True) create_time = Column(DateTime, default=datetime.utcnow, nullable=False) update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) is_deleted = Column(Boolean, default=False, nullable=False) delete_time = Column(DateTime, nullable=True) class Teacher(Base, BaseModel): __tablename__ = "teachers" num = Column(String(30), unique=True, nullable=False, index=True, comment="老师编号") name = Column(String(30), nullable=False, default='') age = Column(Integer, default=18) sex = Column(Integer, default=0, comment="0未知 1男 2女") home_place = Column(String(50), comment="家乡") college = Column(String(50), comment="毕业院校") specialty = Column(String(50), comment="专业") enrollment_time = Column(DateTime, comment="入学时间") graduate_time = Column(DateTime, comment="毕业时间") education = Column(String(50), comment="学历") work_experience = Column(Text, comment="工作经验") coach_area = Column(String(50), comment="教学方向:应用,项目,算法") head_teacher_of = relationship("Class", foreign_keys="Class.head_teacher_id", back_populates="head_teacher") coach_teacher_of = relationship("Class", foreign_keys="Class.coach_teacher_id", back_populates="coach_teacher") tutor_teacher_of = relationship("Class", foreign_keys="Class.tutor_teacher_id", back_populates="tutor_teacher") advisor_of = relationship("Student", foreign_keys="Student.advisor_id", back_populates="advisor") class Class(Base, BaseModel): __tablename__ = "classes" num = Column(String(30), unique=True, nullable=False, index=True, comment="班级编号") name = Column(String(30), nullable=False, comment="班级名称") class_start_time = Column(DateTime, comment="开课时间") head_teacher_id = Column(Integer, ForeignKey("teachers.id"), comment="班主任") coach_teacher_id = Column(Integer, ForeignKey("teachers.id"), comment="授课老师") tutor_teacher_id = Column(Integer, ForeignKey("teachers.id"), comment="助教老师") head_teacher = relationship("Teacher", foreign_keys=[head_teacher_id], back_populates="head_teacher_of") coach_teacher = relationship("Teacher", foreign_keys=[coach_teacher_id], back_populates="coach_teacher_of") tutor_teacher = relationship("Teacher", foreign_keys=[tutor_teacher_id], back_populates="tutor_teacher_of") students = relationship("Student", back_populates="klass") employments = relationship("Employment", back_populates="klass") class Student(Base, BaseModel): __tablename__ = "students" num = Column(String(30), unique=True, nullable=False, index=True, comment="学生编号") name = Column(String(30), nullable=False, default='') age = Column(Integer, default=18) sex = Column(Integer, default=0, comment="0未知 1男 2女") home_place = Column(String(50), comment="家乡") college = Column(String(50), comment="毕业院校") specialty = Column(String(50), comment="专业") enrollment_time = Column(DateTime, comment="入学时间") graduate_time = Column(DateTime, comment="毕业时间") education = Column(String(50), comment="学历") class_id = Column(Integer, ForeignKey("classes.id"), nullable=False, default=0, index=True) advisor_id = Column(Integer, ForeignKey("teachers.id"), comment="顾问id") klass = relationship("Class", back_populates="students") advisor = relationship("Teacher", foreign_keys=[advisor_id], back_populates="advisor_of") scores = relationship("Score", back_populates="student") employment = relationship("Employment", back_populates="student", uselist=False) class Score(Base, BaseModel): __tablename__ = "scores" sid = Column(Integer, ForeignKey("students.id"), nullable=False, index=True, comment="学生ID") num = Column(String(30), nullable=False, comment="考核序次") score = Column(DECIMAL(10, 2), nullable=False, comment="分数") student = relationship("Student", back_populates="scores") __table_args__ = ( UniqueConstraint('sid', 'num', name='uk_sid_score'), Index('idx_sid', 'sid'), ) class Employment(Base, BaseModel): __tablename__ = "employment" sid = Column(Integer, ForeignKey("students.id"), unique=True, comment="学生ID") class_id = Column(Integer, ForeignKey("classes.id"), nullable=False, index=True, comment="班级ID") employment_open_time = Column(DateTime, comment="就业开放时间") offer_recived_time = Column(DateTime, comment="offer下发时间") employment_company = Column(String(50), comment="就业公司") employment_salary = Column(DECIMAL(20, 5), comment="就业薪资") student = relationship("Student", back_populates="employment") klass = relationship("Class", back_populates="employments") __table_args__ = ( UniqueConstraint('sid', name='uk_sid'), Index('idx_class_id', 'class_id'), )