25 lines
578 B
Python
25 lines
578 B
Python
from schemas.student import *
|
|
from core.database import get_db
|
|
from models.student import *
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
def create_students(d: dict, db: Session):
|
|
stu = Student(**d)
|
|
|
|
try:
|
|
db.add(stu)
|
|
# 把 SQL 发给数据库执行
|
|
db.flush()
|
|
# 根据 id 生成学生编号
|
|
if not stu.student_no:
|
|
stu.student_no = f"STU{stu.id:04d}"
|
|
db.commit()
|
|
db.refresh(stu)
|
|
return stu
|
|
except Exception as e:
|
|
db.rollback()
|
|
print(f"插入学生失败: {e}")
|
|
return None
|
|
|