之前 (await ...).scalars() 链式在 SQLAlchemy 2.0 async 下报 'coroutine' has no attribute 'scalars' 错误。改为先 await 拿 result 再 .scalars(),这是 SQLAlchemy 2.0 推荐的 async 写法。
25 lines
830 B
Python
25 lines
830 B
Python
"""/sources 源列表(只读,所有登录用户可看)。"""
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import get_current_user
|
|
from app.database import get_session
|
|
from app.models.source import Source
|
|
from app.models.user import User
|
|
from app.schemas.source import SourceOut
|
|
|
|
router = APIRouter(prefix="/sources", tags=["sources"])
|
|
|
|
|
|
@router.get("", response_model=list[SourceOut])
|
|
async def list_sources(
|
|
user: User = Depends(get_current_user), # noqa: ARG001
|
|
session: AsyncSession = Depends(get_session),
|
|
):
|
|
result = await session.execute(select(Source).order_by(Source.priority.desc(), Source.name))
|
|
rows = result.scalars()
|
|
return [SourceOut.model_validate(s) for s in rows]
|