diff --git a/backend/app/ledger/issue.py b/backend/app/ledger/issue.py index 43815e09..b56ed9f1 100644 --- a/backend/app/ledger/issue.py +++ b/backend/app/ledger/issue.py @@ -144,9 +144,12 @@ async def issued_today(session: Any) -> int: async def issued_runs(session: Any, run_id: str) -> list[Run]: - """这次运行按口径出具过的正式运行(origin.source_run 是它),新的在前。origin_kind 有索引,先按它筛再看 origin。""" - rows = (await session.execute(select(Run).where(Run.origin_kind == ORIGIN).order_by(Run.created_at.desc()))).scalars() - return [r for r in rows if isinstance(r.origin, dict) and r.origin.get("source_run") == run_id] + """这次运行按口径出具过的正式运行(origin.source_run 是它),新的在前。origin_kind 有索引,先按它筛再看 origin + (json_extract,同 auth/keys 认幂等键的写法)。""" + rows = (await session.execute(select(Run).where( + Run.origin_kind == ORIGIN, func.json_extract(Run.origin, "$.source_run") == run_id) + .order_by(Run.created_at.desc()))).scalars() + return list(rows) def issued_view(run: Run) -> dict[str, Any]: diff --git a/backend/app/ledger/reconcile.py b/backend/app/ledger/reconcile.py index 4e9daf26..722573bb 100644 --- a/backend/app/ledger/reconcile.py +++ b/backend/app/ledger/reconcile.py @@ -131,8 +131,12 @@ def actions_of(content: dict[str, Any], names: dict[str, str], hide: Any = None) async def reconciled_runs(session: Any, run_id: str) -> list[Run]: """对这次运行对过的账(origin.source_run 是它),新的在前。""" - rows = (await session.execute(select(Run).where(Run.origin_kind == ORIGIN).order_by(Run.created_at.desc()))).scalars() - return [r for r in rows if isinstance(r.origin, dict) and r.origin.get("source_run") == run_id] + from sqlalchemy import func + + rows = (await session.execute(select(Run).where( + Run.origin_kind == ORIGIN, func.json_extract(Run.origin, "$.source_run") == run_id) + .order_by(Run.created_at.desc()))).scalars() + return list(rows) def reconciled_view(run: Run) -> dict[str, Any]: