这是我们组的项目
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
from .advisor_model import Advisor
|
||||
from .class_model import Classes
|
||||
# from .demo import Student
|
||||
from .employment_model import Employment
|
||||
from .score_model import Score
|
||||
# from .score_model import Score
|
||||
from .student_model import Student
|
||||
from .teacher_model import Teacher
|
||||
|
||||
__all__ = [ "Advisor",'Student',"Employment","Classes","Teacher","Score"]
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import enum
|
||||
|
||||
from sqlalchemy import Column, Enum as SQLEnum, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base
|
||||
|
||||
|
||||
# 性别枚举:只允许 男 / 女
|
||||
class GenderEnum(str, enum.Enum):
|
||||
MALE = "男"
|
||||
FEMALE = "女"
|
||||
|
||||
|
||||
# 本文件只定义 advisor 表的结构,映射到 MySQL 数据库。
|
||||
# 注意:这里只放「表长什么样」,不建表、不插数据、不创建会话。
|
||||
|
||||
class Advisor(Base):
|
||||
# 顾问表 表名 advisor
|
||||
__tablename__ = 'advisor'
|
||||
# 定义字段
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
advisor_name = Column(String(20), unique=True, nullable=False)
|
||||
flag = Column(Integer, nullable=False)
|
||||
# 性别:枚举类型(男/女),可空
|
||||
gender = Column(SQLEnum(GenderEnum, values_callable=lambda e: [m.value for m in e]), nullable=True)
|
||||
# 电话:11位纯数字(校验在 Schema 层做),非必填
|
||||
phone = Column(String(11), nullable=True)
|
||||
students = relationship("Student", back_populates="advisor")
|
||||
|
||||
# 关联 Student(一对多:一个顾问对应多个学生)
|
||||
# 等 student_model.py 里写好 Student 模型、且两边 back_populates 对上后,再取消注释启用。
|
||||
# student = relationship("Student", back_populates="advisor")
|
||||
@@ -0,0 +1,24 @@
|
||||
#导入需要用的工具
|
||||
from sqlalchemy import Column, Integer, String, DateTime
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base
|
||||
from model.teacher_model import teacher_class
|
||||
|
||||
#班级表
|
||||
class Classes(Base):
|
||||
__tablename__ = "classes"
|
||||
cid = Column(Integer, primary_key=True, autoincrement=True)
|
||||
class_name = Column(String(10), nullable=False)
|
||||
start_time = Column(DateTime,nullable=False)
|
||||
head_teacher = Column(String(10),nullable=False)
|
||||
teacher = Column(String(15),nullable=False)
|
||||
is_del = Column(Integer, default=1) # 与现有查询/删除接口一致:1有效,0删除
|
||||
#老师表和班级表多对多
|
||||
teachers = relationship("Teacher", secondary=teacher_class, back_populates="classes")
|
||||
# #班级表和学生表一对多
|
||||
students = relationship("Student", back_populates="classes")
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#
|
||||
# from sqlalchemy import Column, Integer, String, ForeignKey
|
||||
# from sqlalchemy.orm import relationship
|
||||
# from sqlalchemy.sql.sqltypes import DateTime
|
||||
# from database import Base
|
||||
#
|
||||
#
|
||||
# #学生表
|
||||
# class Student(Base):
|
||||
# __tablename__ = "student"
|
||||
# id = Column(Integer, primary_key=True,autoincrement=True) #学生编号
|
||||
# # class_id = Column(Integer,ForeignKey('student_class.id')) #班级ID
|
||||
# advisor=relationship("Advisor",back_populates="students")
|
||||
# student_no = Column(Integer) #学号
|
||||
# name = Column(String(10),nullable=False) #姓名
|
||||
# native_places = Column(String(10),nullable=False) #籍贯
|
||||
# school = Column(String(10),nullable=False) #学校
|
||||
# major = Column(String(10),nullable=False) #专业
|
||||
# enrollment_time = Column(DateTime(timezone=True),nullable=False) #入学时间
|
||||
# graduation_time = Column(DateTime(timezone=True),nullable=False) #毕业时间
|
||||
# education = Column(String(10),nullable=False) #学历
|
||||
# advisor_id = Column(Integer,ForeignKey('advisor.id'),nullable=False) #顾问编号
|
||||
# age = Column(String(10),nullable=False) #年龄
|
||||
# gender = Column(String(10),nullable=False) #性别
|
||||
# status = Column(String(10)) #状态(在读、进入就业、已就业)
|
||||
# flag = Column(Integer,nullable=False) #逻辑删除标记 0表示已经删除 1表示可以删除
|
||||
# scores = relationship("Score",back_populates="student")
|
||||
#
|
||||
@@ -0,0 +1,42 @@
|
||||
# path: model/employment_model.py
|
||||
# time:2026年9月12日10:24
|
||||
# title:就业信息 ORM 模型
|
||||
# author:周兴
|
||||
# info:该文件用于定义就业表在数据库里长什么样,把MySQL的employment表映射成Python里面的Employment ORM类
|
||||
|
||||
from sqlalchemy import Column, Integer, String, DateTime, Float, ForeignKey
|
||||
from database import Base
|
||||
|
||||
class Employment(Base):
|
||||
__tablename__ = "employment" # Python 类对应 MySQL 里面的 employment 表。
|
||||
id = Column(Integer,primary_key=True,autoincrement=True) # 主键
|
||||
student_id = Column(Integer,ForeignKey('student.sid'),nullable=False,unique=True) # 学生ID,外键关联 student表的id
|
||||
employment_start_time = Column(DateTime,nullable=True)
|
||||
|
||||
# offer下发时间
|
||||
offer_time = Column(
|
||||
DateTime,
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# 公司名称
|
||||
company_name = Column(
|
||||
String(100),
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# 薪资
|
||||
salary = Column(
|
||||
Float,
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# 逻辑删除标记:1表示正常,0表示已删除
|
||||
flag = Column(
|
||||
Integer,
|
||||
default=1,
|
||||
nullable=False
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
from sqlalchemy import Column, Integer, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base
|
||||
|
||||
class Score(Base):
|
||||
__tablename__ = "score"
|
||||
id = Column(Integer, primary_key=True,autoincrement=True)
|
||||
student_id = Column(Integer, ForeignKey("student.sid"),nullable=False)
|
||||
exam_id = Column(Integer, nullable=False)
|
||||
score = Column(Integer, nullable=False)
|
||||
flag = Column(Integer,default=1,nullable=False)
|
||||
student=relationship("Student",back_populates="scores")
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
from sqlalchemy import ForeignKey, Column, Integer, String, Date, Table
|
||||
from sqlalchemy.orm import relationship
|
||||
from database import Base
|
||||
from model.teacher_model import teacher_student
|
||||
|
||||
|
||||
# 。创建新学生记录(包含字段:学生编号ID、学生班级、学生姓名、籍贯、毕业院校、专业、入学时间、毕
|
||||
# 业时间、学历、顾问编号、年龄、性别).可选增加字段:状态(在读、进入就业、已就业)
|
||||
# 。查询学生信息(支持按编号、姓名、班级等条件筛选)、
|
||||
# 更新学生信息。
|
||||
# 逻辑删除学生。
|
||||
# 。提示:可设计学号生成规则,而非单一自增,可选扩展:支持上传Excel 文件批量创建学生
|
||||
class Student(Base):
|
||||
__tablename__ = 'student'
|
||||
sid = Column(Integer, primary_key=True, autoincrement=True)
|
||||
student_no=Column(String(50), unique=True, nullable=False)
|
||||
student_name=Column(String(50), nullable=False)
|
||||
native_place=Column(String(200), nullable=True)
|
||||
school=Column(String(100), nullable=True)
|
||||
major=Column(String(50), nullable=True)
|
||||
enrollment_time=Column(Date, nullable=True)
|
||||
graduation_time=Column(Date, nullable=True)
|
||||
education=Column(String(100), nullable=True)
|
||||
age=Column(Integer, nullable=True)
|
||||
gender=Column(String(20), nullable=True)
|
||||
state=Column(String(20),nullable=True)
|
||||
flag=Column(Integer,nullable=False,default=1)
|
||||
class_id = Column(Integer, ForeignKey('classes.cid'), nullable=False)
|
||||
teachers = relationship("Teacher", secondary=teacher_student, back_populates="students")
|
||||
advisor = relationship("Advisor", back_populates="students")
|
||||
advisor_id = Column(Integer, ForeignKey('advisor.id'), nullable=False)
|
||||
scores=relationship("Score",back_populates="student")
|
||||
classes = relationship("Classes", back_populates="students")
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# 从 sqlalchemy 库导入 create_engine,用于创建数据库连接引擎
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, Table,Date
|
||||
from sqlalchemy.orm import relationship
|
||||
from database import Base
|
||||
|
||||
#=============== 多对多中间表1:老师 ↔ 班级 ===============
|
||||
teacher_class = Table(
|
||||
"teacher_class", # 中间表名
|
||||
Base.metadata, # 绑定到 Base 的元数据
|
||||
#两个外键,共同组成联合主键,防止重复绑定同一个老师班级
|
||||
Column("tid",Integer,ForeignKey("teacher.tid"),primary_key=True), # 老师ID,外键指向老师表,同时是组合主键的一部分
|
||||
Column("cid",Integer,ForeignKey("classes.cid"),primary_key=True), # 班级ID,外键指向班级表,同时是组合主键的一部分
|
||||
)
|
||||
|
||||
#=============== 多对多中间表2: 老师 ↔ 学生 ===============
|
||||
teacher_student = Table(
|
||||
"teacher_student", # 中间表名
|
||||
Base.metadata, # 绑定到 Base 的元数据
|
||||
#两个外键,共同组成联合主键,防止重复绑定同一个老师学生
|
||||
Column("tid",Integer,ForeignKey("teacher.tid"),primary_key=True),# 老师ID,外键指向老师表,同时是组合主键的一部分
|
||||
Column("sid",Integer,ForeignKey("student.sid"),primary_key=True),# 学生ID,外键指向学生表,同时是组合主键的一部分
|
||||
)
|
||||
|
||||
#=============== 老师主表 ===============
|
||||
class Teacher(Base):
|
||||
__tablename__ = "teacher" #数据库中对应表名
|
||||
tid = Column(Integer, primary_key=True, autoincrement=True) #主键:老师编号,自增。
|
||||
t_name = Column(String(20), nullable=False) #老师名字,非空,最大20个字符
|
||||
phone = Column(String(11), unique=True) #手机号,唯一
|
||||
subject = Column(String(20)) #授课科目,默认值为“班主任”
|
||||
entry_time = Column(Date) #入职时间
|
||||
is_deleted = Column(Integer, default=1) #逻辑删除,0=已删除,1=未删除
|
||||
#=============== 关联关系定义 ==========
|
||||
# secondary=中间表:指定通过哪张中间表关联
|
||||
# back_populates:反向关联,和Class表中的teachers对应,实现双向查询
|
||||
# 多对多:一个老师对应多个班级
|
||||
classes = relationship("Classes", secondary=teacher_class, back_populates="teachers")
|
||||
# 多对多:一个老师对应多个学生
|
||||
students = relationship("Student", secondary=teacher_student, back_populates="teachers")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user