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

Litestar中SQLAlchemyAsyncRepository调用get_many出现属性错误求助

Litestar框架SQLAlchemyAsyncRepository使用问题

我正在学习Litestar框架,使用SQLAlchemyAsyncRepository时遇到问题。

仓库定义代码

class ReservationRepository(repository.SQLAlchemyAsyncRepository[ReservationModel]):
    model_type = ReservationModel

async def provide_reservation_repo(db_session: AsyncSession) -> ReservationRepository:
    return ReservationRepository(session=db_session)

控制器使用代码

@post("/")
async def reserve(
    self,
    repo: ReservationRepository,
    data: DTOData[Reservation],
) -> Reservation:
    intersections = await repo.get_many(

错误信息

File "room_reservation/app/controllers/reservation.py", line 93, in reserve                 
    intersections = await repo.get_many(                                                      
                          ^^^^^^^^^^^^^                                                       
AttributeError: 'ReservationRepository' object has no attribute 'get_many'. Did you mean: 'add_many'?  

环境信息

  • litestar==2.21.1
  • advanced_alchemy==1.9.3
  • Python 3.14.5

问题原因

你使用的advanced_alchemy==1.9.3版本中,SQLAlchemyAsyncRepository已移除get_many方法,该功能被拆分为两个更明确的方法:list()和get_many_by_ids()。

解决方案

根据业务需求选择对应方法:

  • 获取符合过滤条件的多条记录:使用list()方法,支持过滤、排序、分页等参数
    # 示例:获取状态为"confirmed"的所有预订
    intersections = await repo.list(filter_conditions=(ReservationModel.status == "confirmed",))
    
  • 根据多个ID批量获取记录:使用get_many_by_ids()方法
    # 示例:获取ID列表对应的预订记录
    intersections = await repo.get_many_by_ids(ids=[1, 2, 4])
    

内容的提问来源于stack exchange,提问作者senior_pimiento

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.10 06:03:10