fix: 修复部分数据获取异常

main
chenzhirong 4 months ago
parent 0d518cc13a
commit 9a211f9484

@ -1,12 +1,11 @@
# RG-FastAPI-SQLModel 模板项目 # RG-FastAPI-SQLModel 模板项目
一个基于FastAPI和SQLModel的现代化Python Web应用模板提供了快速开发RESTful API的基础架构。 一个基于FastAPI和SQLModel的现代化Python Web应用模板提供了快速开发RESTful API的基础架构。
## 项目特点 ## 项目特点
- 基于FastAPI构建的高性能异步Web框架 - 基于FastAPI构建的高性能异步Web框架
- 使用SQLModel进行数据库ORM操作结合了SQLAlchemy和Pydantic的优点 - 使用SQLModel进行数据库ORM操作结合了SQLAlchemy和Pydantic的优点
- 完整的项目结构包括路由、服务层、实体模型和DTO - 完整的项目结构,包括路由(自动注册)、服务层、实体模型和DTO
- 内置异常处理机制 - 内置异常处理机制
- 中间件支持,包括数据库会话管理 - 中间件支持,包括数据库会话管理
- 实用工具集合包括日志、文件操作、IP工具等 - 实用工具集合包括日志、文件操作、IP工具等

@ -1,7 +1,7 @@
from sqlalchemy import Column, BigInteger, Select, Delete, Update from sqlalchemy import Column, BigInteger, Select, Delete, Update
from common.global_enums import IsDelete from common.global_enums import IsDelete
from utils import current_timestamp from utils import current_timestamp, get_uuid
# 上下文变量,控制是否启用逻辑删除过滤 # 上下文变量,控制是否启用逻辑删除过滤
_SOFT_DELETE_ENABLED = True _SOFT_DELETE_ENABLED = True
@ -9,7 +9,7 @@ from sqlmodel import SQLModel, Field
class DbBaseModel(SQLModel, table=False): class DbBaseModel(SQLModel, table=False):
id: str = Field(default=None, max_length=32, primary_key=True) id: str = Field(default_factory=get_uuid, max_length=32, primary_key=True)
created_time: int = Field( created_time: int = Field(
default_factory=current_timestamp, default_factory=current_timestamp,
sa_type=BigInteger, sa_type=BigInteger,

@ -1,6 +1,6 @@
from fastapi import APIRouter, Query from fastapi import APIRouter, Query
from entity.dto.UserDto import UserQueryPageReq, UserQueryReq from entity.dto.user_dto import UserQueryPageReq, UserQueryReq
from router import BaseController, unified_resp from router import BaseController, unified_resp
from service.user_service import UserService from service.user_service import UserService

@ -172,7 +172,7 @@ class BaseService(Generic[T]):
update_stmt = cls.model.update().where(cls.model.id == pid).values(**data) update_stmt = cls.model.update().where(cls.model.id == pid).values(**data)
session = cls.get_db() session = cls.get_db()
result = await session.execute(update_stmt) result = await session.execute(update_stmt)
return result.rowcount() return result.rowcount
@classmethod @classmethod
async def update_many_by_id(cls, data_list)->None: async def update_many_by_id(cls, data_list)->None:
@ -183,7 +183,7 @@ class BaseService(Generic[T]):
@classmethod @classmethod
async def get_by_id(cls, pid)->T: async def get_by_id(cls, pid)->T:
stmt = cls.model.select(cls.model.id == pid) stmt = cls.model.select().where(cls.model.id == pid)
session = cls.get_db() session = cls.get_db()
return await session.scalar(stmt) return await session.scalar(stmt)
@ -203,14 +203,14 @@ class BaseService(Generic[T]):
del_stmt = cls.model.delete().where(cls.model.id == pid) del_stmt = cls.model.delete().where(cls.model.id == pid)
session = cls.get_db() session = cls.get_db()
exec_result = await session.execute(del_stmt) exec_result = await session.execute(del_stmt)
return exec_result.rowcount() return exec_result.rowcount
@classmethod @classmethod
async def delete_by_ids(cls, pids)-> int: async def delete_by_ids(cls, pids)-> int:
session = cls.get_db() session = cls.get_db()
del_stmt = cls.model.delete().where(cls.model.id.in_(pids)) del_stmt = cls.model.delete().where(cls.model.id.in_(pids))
result = await session.execute(del_stmt) result = await session.execute(del_stmt)
return result.rowcount() return result.rowcount
@classmethod @classmethod
async def get_data_count(cls, query_params: dict = None) -> int: async def get_data_count(cls, query_params: dict = None) -> int:

Loading…
Cancel
Save