You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastAPI /all接口返回数据量远少于数据库存储量的问题排查求助

FastAPI /all接口返回数据量远少于数据库存储量的问题排查求助

大家好,我正在开发一个FastAPI的/all接口,原本功能是从数据库拉取所有Leads数据返回,但现在遇到了奇怪的问题:数据库里明明存了大约59799条Leads,可Swagger UI返回的结果只有1209条,我折腾了好久都没找到问题根源。

为了排查问题,我已经注释了一部分代码,而且发现如果启用下面这段被注释的代码(直接执行SQL并全量获取数据),应该能拿到完整数据,但这样分页功能就失效了:

# query = text(query)
# x = print("SQL Query:", query)
# result = db.get_db().execute(query)
# columns = result.keys()  # Get the column names
# data = [dict(zip(columns, row)) for row in result.fetchall()]  # Convert rows to dictionaries

下面是我当前的接口完整代码,麻烦各位帮忙看看哪里出问题了:

@router.post("/all", operation_id="get_all_leads")
def get_all_leads(
    paging: paging = None,
    assigned: int | None = None,
    source: int | None = None,
    status: str | None = None,
    additional_filters: int | None = None,
    username: User = Depends(get_current_account),
    customer_id: Annotated[str | None, Header()] = None
):
    '''
    This api gets all the leads
    parameters
    -------
    - filter text -> the text you want to search for
    - limit_per_page -> the amount of data that should be fetched in a single page
    - order by -> order the data by the column name
    - skip -> skips the data (eg : 0,10,20,30)
    - sort -> sort the column (1 : asc , -1 : desc)
    filters
    -------
    - assigned : id of assigned staff
    - source  : source id
    - status : csv of status id (eg : 1,7,4,3)
    - additional_filters : addition filter for the DataTable
    '''
    try:
        if paging is None:
            paging = paging()

        db = LoadDatabase(customer_id)

        filters = {'assigned': assigned,
                   'source': source,
                   "status": status,
                   "additional_filters": additional_filters
                   }

        query = fetch_leads_all(db.get_db(), single_lead=None, filters=filters)

        # query = text(query)
        # x = print("SQL Query:", query)
        # result = db.get_db().execute(query)
        # columns = result.keys()  # Get the column names
        # data = [dict(zip(columns, row)) for row in result.fetchall()]  # Convert rows to dictionaries

        paging_params = df_datatables(dataframe=None,
                                      sql=query,
                                      db_connection=db.get_engine(),
                                      params={
                                          'paging':paging,
                                          'filter': paging.filter_text,
                                          'order_by': paging.order_by,
                                          'sort_asc': assign_order(paging.sort),
                                          'skip': paging.skip,
                                          'limit_per_page': paging.limit_per_page,
                                          "exclude_column_search": []
                                      }
                                      )

        data = []  # Initialize the data variable as an empty list
        #output = paging_params.get_data()
        data = output["dataframe"].to_dict(orient="records")

        return {
            "status": "success",
            "total_record": output["total_record"],
            "data": data
        }

    except HTTPException as e:
        raise e

我怀疑是不是分页相关的处理逻辑有问题?比如默认的limit_per_page是不是设置成了1209?或者df_datatables这个工具在处理SQL查询时自动加了分页限制?有没有可能是skip参数的默认值不对?

麻烦各位帮我分析下问题可能出在哪,谢谢了!

备注:内容来源于stack exchange,提问作者Nilesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 12:04:34