SQLAlchemyAsyncRepository对象没有名为 'get_many' 的属性
我在学习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
解决方案
有针对版本 1.9.3 的文件 async.py 的源码,但并不存在 get_many()。
还有针对最新版1.11.0 的源码,它包含 get_many()
还有针对版本 1.10.0 的源码,它也包含 get_many()
你需要把 advanced_alchemy 更新到版本 1.10.0 或 1.11.0
在较新版本的源码中,我还发现关于命令 list() 的信息,指出它已被弃用,建议使用 get_many()。
它也可能提示在较旧的版本中,可以尝试使用 list() 来替代 get_many()。
async def list(
self,
*filters: Union[StatementFilter, ColumnElement[bool]],
auto_expunge: Optional[bool] = None,
statement: Optional[Select[tuple[ModelT]]] = None,
error_messages: Optional[Union[ErrorMessages, EmptyType]] = Empty,
load: Optional[LoadSpec] = None,
execution_options: Optional[dict[str, Any]] = None,
order_by: Optional[Union[List[OrderingPair], OrderingPair]] = None,
use_cache: bool = True,
bind_group: Optional[str] = None,
**kwargs: Any,
) -> List[ModelT]:
"""Use :meth:`get_many` instead.
.. deprecated:: 1.10.0
"""
...
还有一个页面 1.x Changelog - Advanced Alchemy,在那里我也找到了
1.10.0
Released: 2026-05-23
deprecate list()/list_and_count()
Adds get_many() and get_many_and_count() as the preferred APIs across
repositories, services, memory repositories, query repositories,
and cache managers. The older list() and list_and_count() names remain
available as deprecation wrappers until 2.0.
References: https://github.com/litestar-org/advanced-alchemy/pull/706
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。