feat: optimize code

This commit is contained in:
martsforever
2026-03-20 14:46:25 +08:00
parent 7b9d1253a5
commit ab073d09b3
2 changed files with 6 additions and 6 deletions
+2 -2
View File
@@ -23,7 +23,7 @@ def add_sqlmodel_route(app: FastAPI):
if user_dict.get("id") is None: if user_dict.get("id") is None:
raise HTTPException(status_code=500, detail="Update row missing id") raise HTTPException(status_code=500, detail="Update row missing id")
update_user = (await session.exec(select(LlmUser).where(LlmUser.id == user_dict["id"]))).first() update_user = (await session.execute(select(LlmUser).where(LlmUser.id == user_dict["id"]))).first()
if update_user is None: if update_user is None:
raise HTTPException(status_code=500, detail="Update row not found") raise HTTPException(status_code=500, detail="Update row not found")
@@ -41,7 +41,7 @@ def add_sqlmodel_route(app: FastAPI):
if user_dict.get("id") is None: if user_dict.get("id") is None:
raise HTTPException(status_code=500, detail="Update row missing id") raise HTTPException(status_code=500, detail="Update row missing id")
delete_user: LlmUser = (await session.exec(select(LlmUser).where(LlmUser.id == user_dict["id"]))).first() delete_user: LlmUser = (await session.execute(select(LlmUser).where(LlmUser.id == user_dict["id"]))).first()
if delete_user is None: if delete_user is None:
raise HTTPException(status_code=500, detail="Delete row not found") raise HTTPException(status_code=500, detail="Delete row not found")
+4 -4
View File
@@ -402,7 +402,7 @@ def create_model_service(
if not row_dict.get('id'): if not row_dict.get('id'):
raise HTTPException(status_code=400, detail="ID不能为空") raise HTTPException(status_code=400, detail="ID不能为空")
# 根据id查询要更新的记录 # 根据id查询要更新的记录
update_cls = (await session.exec(self.select_cls().where(Cls.id == row_dict.get('id')))).first() update_cls = (await session.execute(self.select_cls().where(Cls.id == row_dict.get('id')))).scalars().first()
if not update_cls: if not update_cls:
# 若记录不存在,抛出异常 # 若记录不存在,抛出异常
raise HTTPException(status_code=500, detail="Update row not found") raise HTTPException(status_code=500, detail="Update row not found")
@@ -442,7 +442,7 @@ def create_model_service(
# 提取所有待更新记录的id # 提取所有待更新记录的id
update_id_list = [row_dict['id'] for row_dict in row_dict_list] update_id_list = [row_dict['id'] for row_dict in row_dict_list]
# 根据id查询所有待更新的记录 # 根据id查询所有待更新的记录
update_cls_list = (await session.exec(self.select_cls().where(Cls.id.in_(update_id_list)))).all() update_cls_list = (await session.execute(self.select_cls().where(Cls.id.in_(update_id_list)))).all()
# 若查询到的记录数量与待更新数量不一致,说明部分id不存在 # 若查询到的记录数量与待更新数量不一致,说明部分id不存在
if len(update_cls_list) != len(row_dict_list): if len(update_cls_list) != len(row_dict_list):
# 抛出异常并提示不存在的id # 抛出异常并提示不存在的id
@@ -480,7 +480,7 @@ def create_model_service(
await before_delete(row_dict, session) await before_delete(row_dict, session)
# 根据id查询要删除的记录 # 根据id查询要删除的记录
delete_cls = (await session.exec(self.select_cls().where(Cls.id == row_dict.get('id')))).first() delete_cls = (await session.execute(self.select_cls().where(Cls.id == row_dict.get('id')))).scalars().first()
if not delete_cls: if not delete_cls:
# 若记录不存在,返回删除失败 # 若记录不存在,返回删除失败
return False return False
@@ -509,7 +509,7 @@ def create_model_service(
row_id_list = [row_dict.get("id") for row_dict in row_dict_list] row_id_list = [row_dict.get("id") for row_dict in row_dict_list]
# 根据id查询所有待删除的记录 # 根据id查询所有待删除的记录
delete_cls_list = (await session.exec(self.select_cls().where(Cls.id.in_(row_id_list)))).all() delete_cls_list = (await session.execute(self.select_cls().where(Cls.id.in_(row_id_list)))).all()
# 若查询到的记录数量与待删除数量不一致,说明部分id不存在 # 若查询到的记录数量与待删除数量不一致,说明部分id不存在
if len(delete_cls_list) != len(row_id_list): if len(delete_cls_list) != len(row_id_list):
# 抛出异常并提示不存在的id # 抛出异常并提示不存在的id