Files
stu/model/student_info_region_model.py
2026-09-23 09:46:15 +08:00

95 lines
5.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 从 database 模块导入 Base(ORM 基类)和 engine(数据库引擎,别名为 db_engine)
from database import Base, engine as db_engine
# 从 sqlalchemy 导入 Enum,并别名为 SAEnum,用于定义数据库枚举列
from sqlalchemy import Enum as SAEnum, Integer, String
# 从 sqlalchemy 导入常用组件:inspect(检查表是否存在)、Column(列)、
# VARCHAR/BIGINT/Date/DateTime(字段类型)、func(SQL 函数,如 now())
from sqlalchemy import inspect, Column, VARCHAR, BIGINT, Date, DateTime, func
# 从自定义枚举模块导入学生状态、性别、学历层次、逻辑删除枚举
from enums import StudentStatusEnum, GenderEnum, EducationLevelEnum, IsDeletedEnum
def _sa_enum(e):
"""
统一让 SQLAlchemy 存枚举的 value 而不是 name。
默认 SQLAlchemy 的 Enum 存的是枚举成员名(如 'male'),
这里通过 values_callable 指定存枚举的值(如 '1'、'0')。
"""
return SAEnum(e, values_callable=lambda x: [m.value for m in x])
# ======================== 学生 ==========================
# 学生信息表模型,继承 Base(declarative_base 生成的基类)
class Student_info(Base):
__tablename__ = 'student_info' # 数据库表名
# 表参数:指定字符集和排序规则为 utf8mb4
__table_args__ = {'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_general_ci'}
# 学号,主键,非空,长度 20
student_id = Column(VARCHAR(20), primary_key=True, nullable=False, comment='学号,收紧长度')
# 学生姓名,非空,长度 50
student_name = Column(VARCHAR(50), nullable=False, comment='学生姓名')
# 性别,枚举类型(存 value:'0'=女,'1'=男),非空
gender = Column(Integer, nullable=False, comment='性别:0=女,1=男')
# 身份证号码,唯一,非空,长度 18
id_card = Column(VARCHAR(18), unique=True, nullable=False, comment='身份证号码,收紧至18位')
# 出生日期,可空
birthday = Column(Date, nullable=True, comment='出生日期')
# 民族,可空,长度 20
ethnicity = Column(VARCHAR(20), nullable=True, comment='民族')
# 籍贯地区编码,可空,带索引(注释说外键关联 region_dimension,但模型里未定义 ForeignKey)
region_id = Column(VARCHAR(12), nullable=True, index=True,
comment='籍贯地区编码,外键关联地区维度表(region_dimension)')
# 手机号码,可空,长度 11
phone = Column(VARCHAR(11), nullable=True, comment='手机号码,收紧至11位')
# 毕业学校,可空,长度 20
graduation_school = Column(VARCHAR(20), nullable=True, comment='毕业学校')
# 就读专业,非空,长度 20
major = Column(VARCHAR(20), nullable=False, comment='就读专业')
# 所在班级,非空,长度 20,带索引(注释说外键关联 class_info,但模型里未定义 ForeignKey)
class_id = Column(VARCHAR(20), nullable=False, index=True, comment='所在班级,外键关联班级表(class_info)')
# 入学日期,可空
enrollment_date = Column(Date, nullable=True, comment='入学日期')
# 毕业日期,可空
graduation_date = Column(Date, nullable=True, comment='毕业日期')
# 学籍状态,枚举,非空:0=在读,1=休学,2=退学,3=毕业,4=结业
student_status = Column(Integer, nullable=False,comment='学籍状态:0=在读,1=休学,2=退学,3=毕业,4=结业')
# 学历层次,枚举,非空:1=大专,2=本科,3=硕士研究生,4=博士研究生
education_level = Column(Integer, nullable=False,comment='学历层次:1=大专,2=本科,3=硕士研究生,4=博士研究生')
# 逻辑删除标志,枚举,非空,默认值为 IsDeletedEnum.no('0'=未删除)
is_deleted = Column(Integer, default=IsDeletedEnum.no, nullable=False,comment='逻辑删除:0=未删除,1=已删除')
# 创建时间,非空,服务器默认值为当前时间(数据库端生成)
create_time = Column(DateTime, nullable=False, server_default=func.now(), comment='创建时间')
# 更新时间,非空,服务器默认值为当前时间,更新时自动刷新为当前时间
update_time = Column(DateTime, nullable=False, server_default=func.now(), onupdate=func.now(),
comment='更新时间')
# ======================== 地区 ==========================
# 地区维度表模型
class Region_dimension(Base):
__tablename__ = 'region_dimension' # 表名
# 表参数:utf8mb4 字符集
__table_args__ = {'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_general_ci'}
# 自增主键,BIGINT 类型
id = Column(BIGINT, primary_key=True, autoincrement=True, comment='自增主键')
# 地区编码,唯一,非空,长度 12
code = Column(VARCHAR(12), unique=True, nullable=False, comment='地区编码')
# 省 / 直辖市,非空
province = Column(VARCHAR, nullable=False, comment='省 / 直辖市')
# 市,非空
city = Column(VARCHAR, nullable=False, comment='市')
# 区 / 县,非空
district = Column(VARCHAR, nullable=False, comment='区 / 县')
# 判断表是否存在,不存在则创建表
# 检查 student_info 表是否存在
if not inspect(db_engine).has_table('student_info'):
# 创建所有继承 Base 的表(此处会创建 student_info 和 region_dimension)
Base.metadata.create_all(db_engine)
# 检查 region_dimension 表是否存在
if not inspect(db_engine).has_table('region_dimension'):
Base.metadata.create_all(db_engine)