You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
2.5 KiB

from sqlalchemy import func
from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import Column, ForeignKey
from sqlalchemy.sql.sqltypes import Integer, String, FLOAT, DateTime, Boolean
from pick_student.database import Base, engine
class Student(Base): # 类名使用大写
__tablename__ = "student"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
student_id = Column(Integer, nullable=False, comment='学号')
name = Column(String, nullable=False, comment='姓名')
scores = Column(FLOAT, comment='得分', default=0)
consecutive_calls = Column(Integer, default=0) # 记录连续被点名的次数
is_master = Column(Boolean, default=False) # 是否进入知识大师状态
master_uses = Column(Integer, default=0) # 知识大师状态下的剩余次数
class_name = Column(String, ForeignKey('class.class_name'), nullable=False)
user_name = Column(String, ForeignKey('teacher.user_name'), nullable=False)# 外键引用修正
class_ = relationship("Class", back_populates="students")
created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')
__mapped_args__ = {"order_by": id}
class Teacher(Base):
__tablename__ = "teacher"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
user_name = Column(String, nullable=False, comment='教师用户名')
password_hash = Column(String, nullable=False, comment='密码')
created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')
classes = relationship("Class", back_populates="teacher")
# 班级表
class Class(Base):
__tablename__ = "class"
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
class_name = Column(String, nullable=False, comment='班级名称')
class_time = Column(String, nullable=False, comment='开班时间')
user_name = Column(String, ForeignKey('teacher.user_name'), nullable=False)
teacher = relationship("Teacher", back_populates="classes")
students = relationship("Student", back_populates="class_") # 使用 class_ 避免与类名冲突
created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')