From 21317efe78188e7a97f96793e4dfab2c21c7b0b8 Mon Sep 17 00:00:00 2001 From: wolin_liyujie <168155038@qq.com> Date: Mon, 21 Sep 2026 11:17:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BB=BA=E5=A5=BD=E4=BA=86=E5=AD=A6?= =?UTF-8?q?=E7=94=9F=E8=A1=A8=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/database.py | 2 +- models/student.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/database.py b/core/database.py index ae95c99..cb3bab3 100644 --- a/core/database.py +++ b/core/database.py @@ -17,7 +17,7 @@ IP:localhost from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker,declarative_base -from .config import ( +from core.config import ( database_connect_user, database_connect_password, database_connect_ip, diff --git a/models/student.py b/models/student.py index e69de29..990a693 100644 --- a/models/student.py +++ b/models/student.py @@ -0,0 +1,45 @@ +from core.database import Base +from sqlalchemy import * +from datetime import datetime + +class Student(Base): + __tablename__ = 'student_info_detail' + id = Column(Integer + , primary_key=True + , autoincrement=True + , comment = '学生编号,自增主键' + ) + student_no = Column(VARCHAR(50) + , unique = True) + student_name = Column(VARCHAR(50)) + class_id = Column(Integer + ,foreign_key = 'class_info_detail.id' + )#外键约束 班级表的主键id,存在这个班级,学生表才可以输入 + consultant_id = Column(Integer + ,nullable = True + ,foreign_key = 'consultant_info_detail.id' + )#外键约束 顾问表的主键id,存在这个顾问,才可以写入,允许为空 + native_place = Column(VARCHAR(100) + ,nullable = True) + graduation_school = Column(VARCHAR(100) + ,nullable = True) + major = Column(VARCHAR(100) + ,nullable = True) + enrollment_date = Column(Date + ,nullable = False)#入学时间必填 + graduation_date = Column(Date, + nullable = True)#毕业时间可以为空 + education = Column(VARCHAR(50) + ,nullable = True)#学历可以为空 + age = Column(Integer + ,nullable = False) + gender = Column(VARCHAR(10) + ,nullable = False) + create_at = Column(DATETIME + ,default = datetime.now) + update_at = Column(DATETIME + ,default = datetime.now + ,onupdate = datetime.now) + is_deleted = Column(Integer + ,default = 0)#逻辑删除,默认为0,为1则表示已删除 + From 546bc68847af42e76973c6b71379f9244aa4433c Mon Sep 17 00:00:00 2001 From: wolin_liyujie <168155038@qq.com> Date: Mon, 21 Sep 2026 12:36:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=86=99=E5=A5=BD=E4=BA=86=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AD=A6=E7=94=9F=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AD=A6=E7=94=9Fdao=E5=B1=82=EF=BC=8Cmain=E9=87=8C?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E4=BA=86student=E2=80=94=E2=80=94router?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/student.py | 14 ++++++++++++++ dao/student.py | 16 ++++++++++++++++ main.py | 3 ++- schemas/student.py | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/api/student.py b/api/student.py index e69de29..9299659 100644 --- a/api/student.py +++ b/api/student.py @@ -0,0 +1,14 @@ +from fastapi import APIRouter,Depends,HTTPException +from sqlalchemy.orm import Session +from dao.student import * +from schemas.student import StudentCreate + +student_router = APIRouter() + +@student_router.post("/student") +def create_student(request:StudentCreate,db:Session,create = Depends(create_students)): + d = request.model_dump() + r = create_students(d,db) + if not r: + raise HTTPException(status_code=500,detail='服务器繁忙,请稍后添加!') + return {'code':200,'detail':"添加学生成功"} \ No newline at end of file diff --git a/dao/student.py b/dao/student.py index e69de29..9014fe5 100644 --- a/dao/student.py +++ b/dao/student.py @@ -0,0 +1,16 @@ +from schemas.student import * +from core.database import get_db +from models.student import * +from utils.code_generator import generate_no + + +def create_students(d,db): + stu = Student(**d) + try: + db.add(stu) + generate_no("student_no", "STU", stu, db) + db.commit() + except:return False + else: + return True + diff --git a/main.py b/main.py index fe32962..377063e 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,14 @@ from fastapi import FastAPI from api.example import example_router from middleware import log_middleware import uvicorn +from api.student import student_router app = FastAPI() app.middleware("http")(log_middleware) #-------------朱婷婷-------------- #-------------李玉杰-------------- - +app.include_router(student_router) #-------------张义--------------- #-------------薄鑫--------------- diff --git a/schemas/student.py b/schemas/student.py index e69de29..d4a044a 100644 --- a/schemas/student.py +++ b/schemas/student.py @@ -0,0 +1,18 @@ +from pydantic import BaseModel +from datetime import date + +class StudentCreate(BaseModel): + student_no: str|None = None + student_name: str + class_id: int + consultant_id: int | None = None + native_place: str | None = None + graduate_school: str | None = None + major: str | None = None + enrollment_date: date + graduation_date: date | None = None + education: str | None = None + age: int + gender: str + +