Skip to content

Commit c6547be

Browse files
fix: handle detached node instances in update_node_status
1 parent f039f4c commit c6547be

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

app/db/crud/node.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from datetime import datetime, timezone
22
from typing import Optional, Union
33

4-
from sqlalchemy import and_, case, delete, func, select, update, bindparam, or_
4+
from sqlalchemy import and_, bindparam, case, delete, func, or_, select, update
55
from sqlalchemy.ext.asyncio import AsyncSession
6+
from sqlalchemy.exc import InvalidRequestError
67
from sqlalchemy.sql.functions import coalesce
78

89
from app.db.models import (
@@ -348,7 +349,14 @@ async def update_node_status(
348349
)
349350
await db.execute(stmt)
350351
await db.commit()
351-
await db.refresh(db_node)
352+
353+
try:
354+
# Prefer refreshing the existing instance to keep relationships loaded
355+
await db.refresh(db_node)
356+
except InvalidRequestError:
357+
# If the instance was detached (e.g., used across sessions), re-fetch it
358+
db_node = (await db.execute(select(Node).where(Node.id == db_node.id))).scalar_one()
359+
352360
await load_node_attrs(db_node)
353361
return db_node
354362

0 commit comments

Comments
 (0)